﻿using BonjourQuotation.ObjectModel.AccountData;
using BonjourQuotation.ObjectModel.SetupData;
using BonjourQuotation.UI.Models.AccountModel;
using BonjourQuotation.UI.Models.SetupModel;
using BonjourQuotation.UI.Models.PartialModel;
using BonjourQuotation.UI.Models.ReportModel;
using BonjourQuotation.UI.ServiceManagers;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.Reporting.WebForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using BonjourQuotation.UI.Models.Shared;


namespace BonjourQuotation.UI.Controllers
{
    public class SetupController : BaseController
    {
        SetupManager SetupManager;
        #region ManageQuotationController
        [Authorize]
        [HttpGet]
        public ActionResult SetupManageQuotation()
        {
            SetupManager = new SetupManager();
            var SetupManageQuotationsData = SetupManager.LoadManageQuotations(1);
            SetupManageQuotationsGridViewModel SetupManageQuotationsView = new SetupManageQuotationsGridViewModel();
            SetupManageQuotationsView.ConfirmedSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var conf in SetupManageQuotationsData.Confirmed)
                SetupManageQuotationsView.ConfirmedSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = conf.Value, Value = conf.Key.ToString() });
            SetupManageQuotationsView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupManageQuotationsData.Active)
                SetupManageQuotationsView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupManageQuotationsView, AllPermVModel);
            return View("SetupManageQuotations", SetupManageQuotationsView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadManageQuotationsGrid(SetupManageQuotationsGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupManageQuotationsGridBData SetupManageQuotationsData = new SetupManageQuotationsGridBData();
            if (model.IsSearch)
            {
                SetupManageQuotationsGridBData SearchManageQuotationsData = new SetupManageQuotationsGridBData();
                SearchManageQuotationsData = InitSearchManageQuotation(model);
                SetupManageQuotationsData = SetupManager.LoadSearchedManageQuotations(SearchManageQuotationsData);
            }
            else
                SetupManageQuotationsData = SetupManager.LoadManageQuotations(model.PageNum);
            ModelState.Clear();
            var SetupManageQuotationsView = InitSetupManageQuotationsVModel(SetupManageQuotationsData, model.PageNum);
            this.SetPermission(SetupManageQuotationsView, AllPermVModel);
            return PartialView("LoadManageQuotationsGrid", SetupManageQuotationsView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveManageQuotationDetailData(SetupManageQuotationsGridViewModel model)
        {
            SetupManager = new SetupManager();
            var ManageQuotationData = InitSetupManageQuotationsGridBData(model);
            var EditError = SetupManager.SaveManageQuotationDetailsData(ManageQuotationData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        #region private methods
        private SetupManageQuotationsGridViewModel InitSetupManageQuotationsVModel(SetupManageQuotationsGridBData SetupManageQuotationsData, int PageNum)
        {
            SetupManageQuotationsGridViewModel SetManageQuotationsVModel = new SetupManageQuotationsGridViewModel();
            SetManageQuotationsVModel.Rows = new List<SetupManageQuotationsGridRowViewModel>();
            foreach (var Quota in SetupManageQuotationsData.Rows)
                SetManageQuotationsVModel.Rows.Add(new SetupManageQuotationsGridRowViewModel
                {
                    QuotaID = Quota.QuotaID.ToString(),
                    QuotaCode = Quota.QuotaCode,
                    QuotaName = Quota.QuotaName,
                    IsConfirmed = Quota.Confirmed != null ? Convert.ToBoolean(Quota.Confirmed) : false,
                    IsActive = Quota.Active != null ? Convert.ToBoolean(Quota.Active) : false,
                });
            SetManageQuotationsVModel.ConfirmedSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var conf in SetupManageQuotationsData.Confirmed)
                SetManageQuotationsVModel.ConfirmedSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = conf.Value, Value = conf.Key.ToString() });
            SetManageQuotationsVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupManageQuotationsData.Active)
                SetManageQuotationsVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (SetManageQuotationsVModel.Rows.Count == SetupManageQuotationsGridBData.PageCount * PageNum)
                SetManageQuotationsVModel.IsCanView = true;
            else
                SetManageQuotationsVModel.IsCanView = false;
            return SetManageQuotationsVModel;
        }

        private SetupManageQuotationsGridBData InitSearchManageQuotation(SetupManageQuotationsGridViewModel Data)
        {
            SetupManageQuotationsGridBData SrchData = new SetupManageQuotationsGridBData
            {
                QuotaID = Data.QuotaIDText != null ? Convert.ToInt32(Data.QuotaIDText) : (int?)null,
                QuotaCode = Data.QuotaCodeText,
                QuotaName = Data.QuotaNameText,
                SelectedConfirmed = Data.IsConfirmedSearch,
                SelectedActive = Data.IsActiveSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private SetupManageQuotationsGridBData InitSetupManageQuotationsGridBData(SetupManageQuotationsGridViewModel data)
        {
            SetupManageQuotationsGridBData SetManageQuotationsBData = new SetupManageQuotationsGridBData();
            SetManageQuotationsBData.Rows = new List<SetupManageQuotationsGridRowBData>();
            foreach (var Quota in data.Rows)
                SetManageQuotationsBData.Rows.Add(new SetupManageQuotationsGridRowBData
                {
                    QuotaID = Convert.ToInt32(Quota.QuotaID),
                    Confirmed = Quota.IsConfirmed,
                    Active = Quota.IsActive,
                });
            return SetManageQuotationsBData;
        }
        #endregion
        #endregion
        #region ManageQuotationGroupsController
        [Authorize]
        [HttpGet]
        public ActionResult SetupManageQuotationGroup()
        {
            SetupManager = new SetupManager();
            var SetupManageQuotationGroupsData = SetupManager.LoadManageQuotationGroups(1);
            SetupManageQuotationGroupsGridViewModel SetupManageQuotationGroupsView = new SetupManageQuotationGroupsGridViewModel();
            SetupManageQuotationGroupsView.StatusID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var status in SetupManageQuotationGroupsData.GroupStatus)
                SetupManageQuotationGroupsView.StatusID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = status.Value, Value = status.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupManageQuotationGroupsView, AllPermVModel);
            return View("SetupManageQuotationGroups", SetupManageQuotationGroupsView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadManageQuotationGroupsGrid(SetupManageQuotationGroupsGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupManageQuotationGroupsGridBData SetupManageQuotationGroupsData = new SetupManageQuotationGroupsGridBData();
            if (model.IsSearch)
            {
                SetupManageQuotationGroupsGridBData SearchManageQuotationGroupsData = new SetupManageQuotationGroupsGridBData();
                SearchManageQuotationGroupsData = InitSearchManageQuotationGroups(model);
                SetupManageQuotationGroupsData = SetupManager.LoadSearchedManageQuotationGroups(SearchManageQuotationGroupsData);
            }
            else
                SetupManageQuotationGroupsData = SetupManager.LoadManageQuotationGroups(model.PageNum);
            ModelState.Clear();
            var SetupManageQuotationGroupsView = InitSetupManageQuotationGroupsVModel(SetupManageQuotationGroupsData, model.PageNum);
            this.SetPermission(SetupManageQuotationGroupsView, AllPermVModel);
            return PartialView("LoadManageQuotationGroupsGrid", SetupManageQuotationGroupsView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveManageQuotationGroupDetailData(SetupManageQuotationGroupsGridViewModel model)
        {
            SetupManager = new SetupManager();
            var ManageQuotationGroupData = InitSetupManageQuotationGroupsGridBData(model);
            var EditError = SetupManager.SaveManageQuotationGroupDetailsData(ManageQuotationGroupData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        #region private methods
        private SetupManageQuotationGroupsGridViewModel InitSetupManageQuotationGroupsVModel(SetupManageQuotationGroupsGridBData SetupManageQuotationGroupsData, int PageNum)
        {
            SetupManageQuotationGroupsGridViewModel SetManageQuotationGroupsVModel = new SetupManageQuotationGroupsGridViewModel();
            SetManageQuotationGroupsVModel.Rows = new List<SetupManageQuotationGroupsGridRowViewModel>();
            foreach (var Groups in SetupManageQuotationGroupsData.Rows)
                SetManageQuotationGroupsVModel.Rows.Add(new SetupManageQuotationGroupsGridRowViewModel
                {
                    GroupID = Groups.GroupID,
                    QuotaID = Groups.QuotaID.ToString(),
                    GroupCode = Groups.GroupCode,
                    GroupName = Groups.GroupName,
                    SelStatusID = Groups.SelStatusID.ToString(),
                });
            SetManageQuotationGroupsVModel.StatusID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var status in SetupManageQuotationGroupsData.GroupStatus)
                SetManageQuotationGroupsVModel.StatusID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = status.Value, Value = status.Key.ToString() });
            if (SetManageQuotationGroupsVModel.Rows.Count == SetupManageQuotationGroupsGridBData.PageCount * PageNum)
                SetManageQuotationGroupsVModel.IsCanView = true;
            else
                SetManageQuotationGroupsVModel.IsCanView = false;
            return SetManageQuotationGroupsVModel;
        }

        private SetupManageQuotationGroupsGridBData InitSearchManageQuotationGroups(SetupManageQuotationGroupsGridViewModel Data)
        {
            SetupManageQuotationGroupsGridBData SrchData = new SetupManageQuotationGroupsGridBData
            {
                QuotaID = Data.GroupQuotaIDText != null ? Convert.ToInt32(Data.GroupQuotaIDText) : (int?)null,
                SelectedGroupStatus = Data.SelectedStatusIDText != null ? Convert.ToInt32(Data.SelectedStatusIDText) : (int?)null,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private SetupManageQuotationGroupsGridBData InitSetupManageQuotationGroupsGridBData(SetupManageQuotationGroupsGridViewModel data)
        {
            SetupManageQuotationGroupsGridBData SetManageQuotationGroupsBData = new SetupManageQuotationGroupsGridBData();
            SetManageQuotationGroupsBData.Rows = new List<SetupManageQuotationGroupsGridRowBData>();
            foreach (var Group in data.Rows)
                SetManageQuotationGroupsBData.Rows.Add(new SetupManageQuotationGroupsGridRowBData
                {
                    GroupID = Group.GroupID,
                    GroupName = Group.GroupName,
                    SelStatusID = Group.SelStatusID != null ? Convert.ToInt32(Group.SelStatusID) : (int?)null,
                });
            return SetManageQuotationGroupsBData;
        }
        #endregion
        #endregion
        #region CompanyController
        [Authorize]
        [HttpGet]
        public ActionResult SetupCompany()
        {
            SetupManager = new SetupManager();
            var Data = SetupManager.LoadAllCompanies(1);
            ModelState.Clear();
            var ViewModel = InitSetupCompaniesSearchParams(Data);
            this.SetPermission(ViewModel, AllPermVModel);
            return View("SetupCompanies", ViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadCompaniesGrid(SetupCompaniesGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupCompaniesGridBData Data = null;
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteCompanyById(model.CompanyID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupCompaniesGridBData Search = new SetupCompaniesGridBData();
                Search = InitSearchCompany(model);
                Data = SetupManager.LoadSearchedCompanies(Search);
            }
            else
                Data = SetupManager.LoadAllCompanies(model.PageNum);
            ModelState.Clear();
            var ViewModel = InitSetupCompaniesVModel(Data, model.PageNum);
            this.SetPermission(ViewModel, AllPermVModel);
            return PartialView("LoadCompaniesGrid", ViewModel);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadCompanyDetails(int CompanyID)
        {
            SetupManager = new SetupManager();
            var Data = SetupManager.LoadCompanyDetailsByID(CompanyID);
            ModelState.Clear();
            var ViewModel = InitCompanyDetailsViewModel(Data);
            this.SetPermission(ViewModel, AllPermVModel);
            if (CompanyID == 0)
                ViewModel.IsActive = true;
            return PartialView("LoadCompanyDetailsGrid", ViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveCompanyDetailData(SetupCompanyDetailsViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitCompanyDetailsBData(model);
            if (model.CompanyID == 0)
            {
                var CreateError = SetupManager.CreateCompany(Data);
                MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
                {
                    Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
                };
                return PartialView("MessageBox", MessBoxforCreate);
            }
            var EditError = SetupManager.SaveCompanyDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        #region private methods
        private SetupCompaniesGridViewModel InitSetupCompaniesVModel(SetupCompaniesGridBData Data, int PageNum)
        {
            SetupCompaniesGridViewModel VModel = new SetupCompaniesGridViewModel();
            VModel.Rows = new List<SetupCompaniesGridRowViewModel>();
            foreach (var Company in Data.Rows)
                VModel.Rows.Add(new SetupCompaniesGridRowViewModel
                {
                    CompanyID = Company.CompanyID,
                    CompanyName = Company.CompanyName,
                    City = Company.City,
                    CompanyCode = Company.CompanyCode,
                    IsActive=Company.Active !=null ?Convert.ToBoolean(Company.Active):false,
                });
            if (VModel.Rows.Count == SetupCompaniesGridBData.PageCount * PageNum)
                VModel.IsCanView = true;
            else
                VModel.IsCanView = false;
            return VModel;
        }
        private SetupCompaniesGridViewModel InitSetupCompaniesSearchParams(SetupCompaniesGridBData Data)
        {
            SetupCompaniesGridViewModel VModel = new SetupCompaniesGridViewModel();
            VModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                VModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            VModel.CitySearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var city in Data.City)
                VModel.CitySearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Value });
            VModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                VModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            return VModel;
        }
        private SetupCompanyDetailsViewModel InitCompanyDetailsViewModel(SetupCompanyDetailsBData data)
        {
            SetupCompanyDetailsViewModel VModel = new SetupCompanyDetailsViewModel
            {
                CompanyID = data.CompanyID,
                CompanyName = data.CompanyName,
                CompanyCode = data.CompanyCode,
                Address = data.Address,
                SelectedCity = data.SelectedCity,
                CityID = data.CityID.ToString(),
                SelectedStateID = data.SelectedStateID,
                Phone800 = data.Phone800,
                PhoneNumber = data.PhoneNumber,
                FaxNumber = data.FaxNumber,
                PostalCode = data.PostalCode,
                Notes = data.Notes,
                FedID = data.FedID,
                InvComm = data.InvComm,
                ProComm = data.ProComm,
                Brochure_Htl = data.Brochure_Htl,
                Brochure_Rst = data.Brochure_Rst,
                Brochure_Oth = data.Brochure_Oth,
                Contract_Htl = data.Contract_Htl,
                Contract_Rst = data.Contract_Rst,
                Contract_Oth = data.Contract_Oth,
                Resume_Gui = data.Resume_Gui,
                Resume_Emp = data.Resume_Emp,
                ProposalDir = data.ProposalDir,
                IsActive=data.Active !=null ? Convert.ToBoolean(data.Active):false,
            };
            VModel.StateID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var stID in data.StateID)
                VModel.StateID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = stID.Value, Value = stID.Key });
            VModel.City = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var city in data.City)
                VModel.City.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Key.ToString() });
            return VModel;
        }
        private SetupCompaniesGridBData InitSearchCompany(SetupCompaniesGridViewModel Data)
        {
            SetupCompaniesGridBData SrchData = new SetupCompaniesGridBData
            {
                CompanyName = Data.CompanyNameText,
                SelectedCity = Data.CitySearchText,
                CompanyCode = Data.CompanyCodeText,
                SelectedActive=Data.IsActiveSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private SetupCompanyDetailsBData InitCompanyDetailsBData(SetupCompanyDetailsViewModel data)
        {
            SetupCompanyDetailsBData BData = new SetupCompanyDetailsBData
            {
                CompanyID = data.CompanyID,
                CompanyName = data.CompanyName,
                CompanyCode = data.CompanyCode != null ? data.CompanyCode : "-",
                Address = data.Address,
                SelectedCity = data.SelectedCity,
                CityID = (data.CityID != null && data.CityID != "0") ? Convert.ToInt32(data.CityID) : (int?)null,
                SelectedStateID = (data.SelectedStateID == "0" || data.SelectedStateID == null || data.SelectedStateID == "") ? null : data.SelectedStateID,
                Phone800 = data.Phone800,
                PostalCode = data.PostalCode,
                PhoneNumber = data.PhoneNumber,
                FaxNumber = data.FaxNumber,
                Notes = data.Notes,
                FedID = data.FedID,
                InvComm = data.InvComm,
                ProComm = data.ProComm,
                Brochure_Htl = data.Brochure_Htl,
                Brochure_Rst = data.Brochure_Rst,
                Brochure_Oth = data.Brochure_Oth,
                Contract_Htl = data.Contract_Htl,
                Contract_Rst = data.Contract_Rst,
                Contract_Oth = data.Contract_Oth,
                Resume_Gui = data.Resume_Gui,
                Resume_Emp = data.Resume_Emp,
                ProposalDir = data.ProposalDir,
                Active=data.IsActive,
            };
            return BData;
        }
        #endregion
        #endregion
        #region BankController
        [Authorize]
        [HttpGet]
        public ActionResult SetupBank()
        {
            SetupManager = new SetupManager();
            var Data = SetupManager.LoadAllBanks(1);
            ModelState.Clear();
            var ViewModel = InitSetupBanksSearchParams(Data);
            this.SetPermission(ViewModel, AllPermVModel);
            return View("SetupBanks", ViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadBanksGrid(SetupBanksGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupBanksGridBData Data = null;
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteBankById(model.BankID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupBanksGridBData Search = new SetupBanksGridBData();
                Search = InitSearchBank(model);
                Data = SetupManager.LoadSearchedBanks(Search);
            }
            else
                Data = SetupManager.LoadAllBanks(model.PageNum);
            ModelState.Clear();
            var ViewModel = InitSetupBanksVModel(Data, model.PageNum);
            this.SetPermission(ViewModel, AllPermVModel);
            return PartialView("LoadBanksGrid", ViewModel);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult LoadBankDetails(int BankID)
        {
            SetupManager = new SetupManager();
            var Data = SetupManager.LoadBankDetailsByID(BankID);
            ModelState.Clear();
            var ViewModel = InitBankDetailsViewModel(Data);
            this.SetPermission(ViewModel, AllPermVModel);
            if (BankID == 0)
                ViewModel.IsActive = true;
            return PartialView("LoadBankDetailsGrid", ViewModel);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveBankDetailData(SetupBankDetailsViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitBankDetailsBData(model);
            if (model.BankID == 0)
            {
                var CreateError = SetupManager.CreateBank(Data);
                MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
                {
                    Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
                };
                return PartialView("MessageBox", MessBoxforCreate);
            }
            var EditError = SetupManager.SaveBankDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        #region private methods
        private SetupBanksGridViewModel InitSetupBanksVModel(SetupBanksGridBData Data, int PageNum)
        {
            SetupBanksGridViewModel VModel = new SetupBanksGridViewModel();
            VModel.Rows = new List<SetupBanksGridRowViewModel>();
            foreach (var Bank in Data.Rows)
                VModel.Rows.Add(new SetupBanksGridRowViewModel
                {
                    BankID = Bank.BankID,
                    BankName = Bank.BankName,
                    City = Bank.City,
                    BankCode = Bank.BankCode,
                    IsActive=Bank.Active!=null?Convert.ToBoolean(Bank.Active):false,
                });
            VModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                VModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (VModel.Rows.Count == SetupBanksGridBData.PageCount * PageNum)
                VModel.IsCanView = true;
            else
                VModel.IsCanView = false;
            return VModel;
        }
        private SetupBanksGridViewModel InitSetupBanksSearchParams(SetupBanksGridBData Data)
        {
            SetupBanksGridViewModel VModel = new SetupBanksGridViewModel();
            VModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                VModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            VModel.CitySearchText = "";
            VModel.CitySearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var city in Data.City)
                VModel.CitySearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Value });
            return VModel;
        }
        private SetupBankDetailsViewModel InitBankDetailsViewModel(SetupBankDetailsBData data)
        {
            SetupBankDetailsViewModel VModel = new SetupBankDetailsViewModel
            {
                BankID = data.BankID,
                BankName = data.BankName,
                BranchName = data.BranchName,
                BankCode = data.BankCode,
                Address = data.Address,
                SelectedCity = data.SelectedCity,
                CityID = data.CityID.ToString(),
                SelectedStateID = data.SelectedStateID,
                PostalCode = data.PostalCode,
                AccNo = data.AccNo,
                ABANo = data.ABANo,
                SwiftCode = data.SwiftCode,
                Phone800 = data.Phone800,
                PhoneNumber = data.PhoneNumber,
                FaxNumber = data.FaxNumber,
                SelectedCountryID = data.SelectedCountryID.ToString(),
                Notes = data.Notes,
                IsActive=data.Active!=null?Convert.ToBoolean(data.Active):false,
            };
            VModel.StateID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var stID in data.StateID)
                VModel.StateID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = stID.Value, Value = stID.Key });
            VModel.CountryID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cnt in data.CountryID)
                VModel.CountryID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cnt.Value, Value = cnt.Key.ToString() });
            VModel.City = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var city in data.City)
                VModel.City.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Key.ToString() });
            return VModel;
        }
        private SetupBanksGridBData InitSearchBank(SetupBanksGridViewModel Data)
        {
            SetupBanksGridBData SrchData = new SetupBanksGridBData
            {
                BankName = Data.BankNameText,
                SelectedCity = Data.CitySearchText,
                BankCode = Data.BankCodeText,
                PageNum = Data.PageNum,
                SelectedActive=Data.IsActiveSearch,
            };
            return SrchData;
        }
        private SetupBankDetailsBData InitBankDetailsBData(SetupBankDetailsViewModel data)
        {
            SetupBankDetailsBData BData = new SetupBankDetailsBData
            {
                BankID = data.BankID,
                BankName = data.BankName,
                BankCode = data.BankCode != null ? data.BankCode : "-",
                BranchName = data.BranchName,
                AccNo = data.AccNo,
                ABANo = data.ABANo,
                Address = data.Address,
                SelectedCity = data.SelectedCity,
                CityID = (data.CityID != null && data.CityID != "0") ? Convert.ToInt32(data.CityID) : (int?)null,
                SelectedStateID = data.SelectedCountryID == "1" ? (data.SelectedStateID == "0" ? null : data.SelectedStateID) : null,
                Phone800 = data.Phone800,
                PostalCode = data.PostalCode,
                SwiftCode = data.SwiftCode,
                PhoneNumber = data.PhoneNumber,
                FaxNumber = data.FaxNumber,
                SelectedCountryID = data.SelectedCountryID != null ? Convert.ToInt32(data.SelectedCountryID) : (int?)null,
                Notes = data.Notes,
                Active=data.IsActive,
            };
            return BData;
        }
        #endregion
        #endregion
        #region PaymentTypeController
        [Authorize]
        [HttpGet]
        public ActionResult SetupPaymentType()
        {
            SetupManager = new SetupManager();
            var SetupPaymentTypesData = SetupManager.LoadPaymentTypes(1);
            SetupPaymentTypesGridViewModel SetupPaymentTypesView = new SetupPaymentTypesGridViewModel();
            SetupPaymentTypesView.BankID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var bank in SetupPaymentTypesData.BankID)
                SetupPaymentTypesView.BankID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = bank.Value, Value = bank.Key.ToString() });
            SetupPaymentTypesView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupPaymentTypesData.Active)
                SetupPaymentTypesView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupPaymentTypesView, AllPermVModel);
            return View("SetupPaymentTypes", SetupPaymentTypesView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadPaymentTypesGrid(SetupPaymentTypesGridViewModel model)
        {
            SetupManager = new SetupManager();

            SetupPaymentTypesGridBData Data = new SetupPaymentTypesGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeletePaymentTypeById(model.PaymentTypeID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupPaymentTypesGridBData Search = new SetupPaymentTypesGridBData();
                Search = InitSearchPaymentType(model);
                Data = SetupManager.LoadSearchedPaymentTypes(Search);
            }
            else
                Data = SetupManager.LoadPaymentTypes(model.PageNum);
            ModelState.Clear();
            var SetupPaymentTypesView = InitSetupPaymentTypesVModel(Data, model.PageNum);
            this.SetPermission(SetupPaymentTypesView, AllPermVModel);
            return PartialView("LoadPaymentTypesGrid", SetupPaymentTypesView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewPaymentType()
        {
            SetupManager = new SetupManager();
            SetupPaymentTypesGridViewModel Data = new SetupPaymentTypesGridViewModel();
            Data.Rows = new List<SetupPaymentTypesGridRowViewModel>();
            Data.Rows.Add(new SetupPaymentTypesGridRowViewModel
            {
                PaymentTypeDescription = "",
                PaymentTypeID = 0,
                BankID=null,
                IsActive = true,
            });
            var SetupBanksData = SetupManager.LoadPaymentTypes(-1);
            Data.BankID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var bank in SetupBanksData.BankID)
                Data.BankID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = bank.Value, Value = bank.Key.ToString() });
            ModelState.Clear();
            return PartialView("AddPaymentTypeGrid", Data);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SavePaymentTypeDetailData(SetupPaymentTypesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSetupPaymentTypesGridBData(model);
            var EditError = SetupManager.SavePaymentTypeDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewPaymentType(SetupPaymentTypesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitPaymentTypeDetailsBData(model);
            var CreateError = SetupManager.CreatePaymentType(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupPaymentTypesGridViewModel InitSetupPaymentTypesVModel(SetupPaymentTypesGridBData Data, int PageNum)
        {
            SetupPaymentTypesGridViewModel SetVModel = new SetupPaymentTypesGridViewModel();
            SetVModel.Rows = new List<SetupPaymentTypesGridRowViewModel>();
            foreach (var PaymentType in Data.Rows)
                SetVModel.Rows.Add(new SetupPaymentTypesGridRowViewModel
                {
                    PaymentTypeID = PaymentType.PaymentTypeID,
                    BankID = PaymentType.PaymentTypeBankID.ToString(),
                    PaymentTypeCode = PaymentType.PaymentTypeCode,
                    PaymentTypeDescription = PaymentType.PaymentTypeDescription,
                    IsActive=PaymentType.Active!=null?Convert.ToBoolean(PaymentType.Active):false,
                });
            SetVModel.BankID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var bank in Data.BankID)
                SetVModel.BankID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = bank.Value, Value = bank.Key.ToString() });
            SetVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                SetVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (SetVModel.Rows.Count == SetupPaymentTypesGridBData.PageCount * PageNum)
                SetVModel.IsCanView = true;
            else
                SetVModel.IsCanView = false;
            return SetVModel;
        }

        private SetupPaymentTypesGridBData InitSearchPaymentType(SetupPaymentTypesGridViewModel Data)
        {
            SetupPaymentTypesGridBData SrchData = new SetupPaymentTypesGridBData
            {
                PaymentTypeBankID = Convert.ToInt32(Data.SelectedPaymentTypeBankIDText),
                PaymentTypeCode = Data.PaymentTypeCodeText,
                PaymentTypeDescription = Data.PaymentTypeDescriptionText,
                PageNum = Data.PageNum,
                SelectedActive = Data.IsActiveSearch,
            };
            return SrchData;
        }
        private SetupPaymentTypesGridBData InitSetupPaymentTypesGridBData(SetupPaymentTypesGridViewModel data)
        {
            SetupPaymentTypesGridBData SetBData = new SetupPaymentTypesGridBData();
            SetBData.Rows = new List<SetupPaymentTypesGridRowBData>();
            foreach (var PaymentType in data.Rows)
                SetBData.Rows.Add(new SetupPaymentTypesGridRowBData
                {
                    PaymentTypeID = PaymentType.PaymentTypeID,
                    PaymentTypeBankID = PaymentType.BankID != null ?Convert.ToInt32(PaymentType.BankID) : (int?)null,
                    PaymentTypeCode = PaymentType.PaymentTypeCode,
                    PaymentTypeDescription = PaymentType.PaymentTypeDescription,
                    Active=PaymentType.IsActive,
                });
            return SetBData;
        }
        private SetupPaymentTypesGridBData InitPaymentTypeDetailsBData(SetupPaymentTypesGridViewModel data)
        {
            SetupPaymentTypesGridBData BData = new SetupPaymentTypesGridBData();
            BData.Rows = new List<SetupPaymentTypesGridRowBData>();
            BData.Rows.Add(new SetupPaymentTypesGridRowBData
            {
                PaymentTypeBankID = data.Rows[0].BankID != null ? Convert.ToInt32(data.Rows[0].BankID) : (int?)null,
                PaymentTypeCode = data.Rows[0].PaymentTypeCode,
                PaymentTypeDescription = data.Rows[0].PaymentTypeDescription,
                Active=data.Rows[0].IsActive
            });
            return BData;
        }
        #endregion
        #endregion
        #region ServiceTypeController
        [Authorize]
        [HttpGet]
        public ActionResult SetupServiceType()
        {
            SetupManager = new SetupManager();
            var SetupServiceTypesData = SetupManager.LoadServiceTypes(1);
            SetupServiceTypesGridViewModel SetupServiceTypesView = new SetupServiceTypesGridViewModel();
            SetupServiceTypesView.VendorTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var VendorType in SetupServiceTypesData.VendorTypeID)
                SetupServiceTypesView.VendorTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = VendorType.Value, Value = VendorType.Key.ToString() });
            SetupServiceTypesView.Per = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Per in SetupServiceTypesData.Per)
                SetupServiceTypesView.Per.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Per.Value, Value = Per.Key.ToString() });
            SetupServiceTypesView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupServiceTypesData.Active)
                SetupServiceTypesView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupServiceTypesView, AllPermVModel);
            return View("SetupServiceTypes", SetupServiceTypesView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadServiceTypesGrid(SetupServiceTypesGridViewModel model)
        {
            SetupManager = new SetupManager();

            SetupServiceTypesGridBData Data = new SetupServiceTypesGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteServiceTypeById(model.ServiceTypeID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupServiceTypesGridBData Search = new SetupServiceTypesGridBData();
                Search = InitSearchServiceType(model);
                Data = SetupManager.LoadSearchedServiceTypes(Search);
            }
            else
                Data = SetupManager.LoadServiceTypes(model.PageNum);
            ModelState.Clear();
            var SetupServiceTypesView = InitSetupServiceTypesVModel(Data, model.PageNum);
            this.SetPermission(SetupServiceTypesView, AllPermVModel);
            return PartialView("LoadServiceTypesGrid", SetupServiceTypesView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewServiceType()
        {
            SetupManager = new SetupManager();
            SetupServiceTypesGridViewModel Data = new SetupServiceTypesGridViewModel();
            Data.Rows = new List<SetupServiceTypesGridRowViewModel>();
            Data.Rows.Add(new SetupServiceTypesGridRowViewModel
            {
                ServiceTypeDescription = "",
                ServiceTypeCode = "",
                VendorTypeID = null,
                ServiceTypeID = 0,
                Per = null,
                IsActive = true,
            });
            var SetupServiceTypesData = SetupManager.LoadServiceTypes(-1);
            Data.VendorTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var VendorType in SetupServiceTypesData.VendorTypeID)
                Data.VendorTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = VendorType.Value, Value = VendorType.Key.ToString() });
            Data.Per = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Per in SetupServiceTypesData.Per)
                Data.Per.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Per.Value, Value = Per.Key.ToString() });
            ModelState.Clear();
            return PartialView("AddServiceTypeGrid", Data);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveServiceTypeDetailData(SetupServiceTypesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSetupServiceTypesGridBData(model);
            var EditError = SetupManager.SaveServiceTypeDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewServiceType(SetupServiceTypesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitServiceTypeDetailsBData(model);
            var CreateError = SetupManager.CreateServiceType(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupServiceTypesGridViewModel InitSetupServiceTypesVModel(SetupServiceTypesGridBData Data, int PageNum)
        {
            SetupServiceTypesGridViewModel SetVModel = new SetupServiceTypesGridViewModel();
            SetVModel.Rows = new List<SetupServiceTypesGridRowViewModel>();
            foreach (var ServiceType in Data.Rows)
                SetVModel.Rows.Add(new SetupServiceTypesGridRowViewModel
                {
                    ServiceTypeID = ServiceType.ServiceTypeID,
                    VendorTypeID = ServiceType.VendorTypeID.ToString(),
                    Per = ServiceType.Per.ToString(),
                    ServiceTypeCode = ServiceType.ServiceTypeCode,
                    ServiceTypeDescription = ServiceType.ServiceTypeDescription,
                    IsActive=ServiceType.Active !=null ? Convert.ToBoolean(ServiceType.Active):false,
                });
            SetVModel.VendorTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var VendorType in Data.VendorTypeID)
                SetVModel.VendorTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = VendorType.Value, Value = VendorType.Key.ToString() });
            SetVModel.Per = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Per in Data.Per)
                SetVModel.Per.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Per.Value, Value = Per.Key.ToString() });
            SetVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                SetVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (SetVModel.Rows.Count == SetupServicesGridBData.PageCount * PageNum)
                SetVModel.IsCanView = true;
            else
                SetVModel.IsCanView = false;
            return SetVModel;
        }
        private SetupServiceTypesGridBData InitSearchServiceType(SetupServiceTypesGridViewModel Data)
        {
            SetupServiceTypesGridBData SrchData = new SetupServiceTypesGridBData
            {
                ServiceTypeVendorTypeID = Data.SelectedServiceTypeVendorTypeIDText != null ? Convert.ToInt32(Data.SelectedServiceTypeVendorTypeIDText) : (int?)null,
                ServiceTypePer = Data.SelectedServiceTypePerText != null ? Convert.ToInt16(Data.SelectedServiceTypePerText) : (short?)null,
                ServiceTypeCode = Data.ServiceTypeCodeText,
                ServiceTypeDescription = Data.ServiceTypeDescriptionText,
                PageNum = Data.PageNum,
                SelectedActive=Data.IsActiveSearch,
            };
            
            return SrchData;
        }
        private SetupServiceTypesGridBData InitSetupServiceTypesGridBData(SetupServiceTypesGridViewModel data)
        {
            SetupServiceTypesGridBData SetBData = new SetupServiceTypesGridBData();
            SetBData.Rows = new List<SetupServiceTypesGridRowBData>();
            foreach (var ServiceType in data.Rows)
                SetBData.Rows.Add(new SetupServiceTypesGridRowBData
                {
                    ServiceTypeID = ServiceType.ServiceTypeID,
                    VendorTypeID = ServiceType.VendorTypeID != null ? Convert.ToInt32(ServiceType.VendorTypeID) : (int?)null,
                    Per = ServiceType.Per != null ? Convert.ToInt16(ServiceType.Per) : (short?)null,
                    ServiceTypeCode = ServiceType.ServiceTypeCode != null ? ServiceType.ServiceTypeCode : "-",
                    ServiceTypeDescription = ServiceType.ServiceTypeDescription,
                    Active=ServiceType.IsActive,
                });
            return SetBData;
        }
        private SetupServiceTypesGridBData InitServiceTypeDetailsBData(SetupServiceTypesGridViewModel data)
        {
            SetupServiceTypesGridBData BData = new SetupServiceTypesGridBData();
            BData.Rows = new List<SetupServiceTypesGridRowBData>();
            BData.Rows.Add(new SetupServiceTypesGridRowBData
            {
                VendorTypeID = data.Rows[0].VendorTypeID != null ? Convert.ToInt32(data.Rows[0].VendorTypeID) : (int?)null,
                Per = data.Rows[0].Per != null ? Convert.ToInt16(data.Rows[0].Per) : (short?)null,
                ServiceTypeCode = data.Rows[0].ServiceTypeCode != null ? data.Rows[0].ServiceTypeCode : "-",
                ServiceTypeDescription = data.Rows[0].ServiceTypeDescription,
                Active=data.Rows[0].IsActive,
            });
            return BData;
        }
        #endregion
        #endregion
        #region ServiceController
        [Authorize]
        [HttpGet]
        public ActionResult SetupService()
        {
            SetupManager = new SetupManager();
            var SetupServicesData = SetupManager.LoadServices(1);
            SetupServicesGridViewModel SetupServicesView = new SetupServicesGridViewModel();
            SetupServicesView.SelectedServiceServiceTypeIDText = "";
            SetupServicesView.ServiceType = new List<System.Web.WebPages.Html.SelectListItem>();
            SetupServicesView.VendorType = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var ServiceType in SetupServicesData.ServiceTypeID)
                SetupServicesView.ServiceType.Add(new System.Web.WebPages.Html.SelectListItem() { Text = ServiceType.Value, Value = ServiceType.Key.ToString() });
            SetupServicesView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupServicesData.Active)
                SetupServicesView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            foreach (var VndType in SetupServicesData.VendorTypes)
                SetupServicesView.VendorType.Add(new System.Web.WebPages.Html.SelectListItem() { Text = VndType.Value, Value = VndType.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupServicesView, AllPermVModel);
            return View("SetupServices", SetupServicesView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadServicesGrid(SetupServicesGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupServicesGridBData Data = new SetupServicesGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteServiceById(model.ServiceID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                var Search = InitSearchService(model);
                Data = SetupManager.LoadSearchedServices(Search);
            }
            else
                Data = SetupManager.LoadServices(model.PageNum);
            ModelState.Clear();
            var SetupServicesView = InitSetupServicesVModel(Data, model.PageNum);
            this.SetPermission(SetupServicesView, AllPermVModel);
            return PartialView("LoadServicesGrid", SetupServicesView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewService()
        {
            SetupManager = new SetupManager();
            SetupServicesGridViewModel Data = new SetupServicesGridViewModel();
            Data.Rows = new List<SetupServicesGridRowViewModel>();
            Data.Rows.Add(new SetupServicesGridRowViewModel
            {
                ServiceDescription = "",
                ServiceCode = "",
                ServiceID = 0,
                ServiceTypeID = 0,
                IsActive = true,
            });
            var SetupServiceTypesData = SetupManager.LoadServices(-1);
            Data.SelectedServiceServiceTypeIDText = "";
            Data.ServiceType = new List<System.Web.WebPages.Html.SelectListItem>();
            Data.VendorType = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var ServiceType in SetupServiceTypesData.ServiceTypeID)
                Data.ServiceType.Add(new System.Web.WebPages.Html.SelectListItem() { Text = ServiceType.Value, Value = ServiceType.Key.ToString() });
            foreach (var VndType in SetupServiceTypesData.VendorTypes)
                Data.VendorType.Add(new System.Web.WebPages.Html.SelectListItem() { Text = VndType.Value, Value = VndType.Key.ToString() });
            ModelState.Clear();
            return PartialView("AddServiceGrid", Data);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveServiceDetailData(SetupServicesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSetupServicesGridBData(model);
            var EditError = SetupManager.SaveServiceDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewService(SetupServicesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitServiceDetailsBData(model);
            var CreateError = SetupManager.CreateService(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupServicesGridViewModel InitSetupServicesVModel(SetupServicesGridBData Data, int PageNum)
        {
            SetupServicesGridViewModel SetVModel = new SetupServicesGridViewModel();
            SetVModel.Rows = new List<SetupServicesGridRowViewModel>();
            foreach (var Service in Data.Rows)
                SetVModel.Rows.Add(new SetupServicesGridRowViewModel
                {
                    ServiceID = Service.ServiceID,
                    ServiceTypeID = Convert.ToInt32(Service.ServiceTypeID),
                    ServiceCode = Service.ServiceCode,
                    ServiceDescription = Service.ServiceDescription,
                    IsActive=Service.Active !=null ?Convert.ToBoolean(Service.Active):false,
                });
            SetVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                SetVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            SetVModel.ServiceType = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var ServiceType in Data.ServiceTypeID)
                SetVModel.ServiceType.Add(new System.Web.WebPages.Html.SelectListItem() { Text = ServiceType.Value, Value = ServiceType.Key.ToString() });
            if (SetVModel.Rows.Count == SetupServicesGridBData.PageCount * PageNum)
                SetVModel.IsCanView = true;
            else
                SetVModel.IsCanView = false;
            return SetVModel;
        }
        private SetupServicesGridBData InitSearchService(SetupServicesGridViewModel Data)
        {
            SetupServicesGridBData SrchData = new SetupServicesGridBData
            {
                ServiceServiceTypeID = Convert.ToInt32(Data.SelectedServiceServiceTypeIDText),
                ServiceCode = Data.ServiceCodeText,
                ServiceDescription = Data.ServiceDescriptionText,
                PageNum = Data.PageNum,
                SelectedActive = Data.IsActiveSearch,
                VendorTypeID = Data.SelectedVendorTypeID,
            };
            return SrchData;
        }
        private SetupServicesGridBData InitSetupServicesGridBData(SetupServicesGridViewModel data)
        {
            SetupServicesGridBData SetBData = new SetupServicesGridBData();
            SetBData.Rows = new List<SetupServicesGridRowBData>();
            foreach (var Service in data.Rows)
                SetBData.Rows.Add(new SetupServicesGridRowBData
                {
                    ServiceID = Service.ServiceID,
                    ServiceTypeID = Service.ServiceTypeID,
                    ServiceCode = Service.ServiceCode,
                    ServiceDescription = Service.ServiceDescription,
                    Active=Service.IsActive,
                });
            return SetBData;
        }
        private SetupServicesGridBData InitServiceDetailsBData(SetupServicesGridViewModel data)
        {
            SetupServicesGridBData BData = new SetupServicesGridBData();
            BData.Rows = new List<SetupServicesGridRowBData>();
            BData.Rows.Add(new SetupServicesGridRowBData
            {
                ServiceTypeID = data.Rows[0].ServiceTypeID,
                ServiceCode = data.Rows[0].ServiceCode,
                ServiceDescription = data.Rows[0].ServiceDescription,
                Active=data.Rows[0].IsActive,
            });
            return BData;
        }
        #endregion
        #endregion
        #region VendorTypeController
        [Authorize]
        [HttpGet]
        public ActionResult SetupVendorType()
        {
            SetupManager = new SetupManager();
            var SetupVendorTypesData = SetupManager.LoadVendorTypes(1);
            SetupVendorTypesGridViewModel SetupVendorTypesView = new SetupVendorTypesGridViewModel();
            SetupVendorTypesView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupVendorTypesData.Active)
                SetupVendorTypesView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupVendorTypesView, AllPermVModel);
            return View("SetupVendorTypes", SetupVendorTypesView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadVendorTypesGrid(SetupVendorTypesGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupVendorTypesGridBData SetupVendorTypesData = new SetupVendorTypesGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteVendorTypeById(model.VendorTypeID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupVendorTypesGridBData SearchVendorTypesData = new SetupVendorTypesGridBData();
                SearchVendorTypesData = InitSearchVendorType(model);
                SetupVendorTypesData = SetupManager.LoadSearchedVendorTypes(SearchVendorTypesData);
            }
            else
                SetupVendorTypesData = SetupManager.LoadVendorTypes(model.PageNum);
            ModelState.Clear();
            var SetupVendorTypesView = InitSetupVendorTypesVModel(SetupVendorTypesData, model.PageNum);
            this.SetPermission(SetupVendorTypesView, AllPermVModel);
            return PartialView("LoadVendorTypesGrid", SetupVendorTypesView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewVendorType()
        {
            SetupVendorTypesGridRowViewModel VendorTypeDetData = new SetupVendorTypesGridRowViewModel();
            VendorTypeDetData.IsActive = true;
            ModelState.Clear();
            return PartialView("AddVendorTypeGrid", VendorTypeDetData);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveVendorTypeDetailData(SetupVendorTypesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var VendorTypeData = InitSetupVendorTypesGridBData(model);
            var EditError = SetupManager.SaveVendorTypeDetailsData(VendorTypeData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewVendorType(SetupVendorTypesGridRowViewModel model)
        {
            SetupManager = new SetupManager();
            var VendorTypeData = InitVendorTypeDetailsBData(model);
            var CreateError = SetupManager.CreateVendorType(VendorTypeData);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupVendorTypesGridViewModel InitSetupVendorTypesVModel(SetupVendorTypesGridBData SetupVendorTypesData, int PageNum)
        {
            SetupVendorTypesGridViewModel SetVendorTypesVModel = new SetupVendorTypesGridViewModel();
            SetVendorTypesVModel.Rows = new List<SetupVendorTypesGridRowViewModel>();
            foreach (var VendorType in SetupVendorTypesData.Rows)
                SetVendorTypesVModel.Rows.Add(new SetupVendorTypesGridRowViewModel
                {
                    VendorTypeID = VendorType.VendorTypeID,
                    VendorTypeCode = VendorType.VendorTypeCode,
                    VendorTypeDescription = VendorType.VendorTypeDescription,
                    IsActive=VendorType.Active !=null?Convert.ToBoolean(VendorType.Active):false,
                });
            SetVendorTypesVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupVendorTypesData.Active)
                SetVendorTypesVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (SetVendorTypesVModel.Rows.Count == SetupVendorTypesGridBData.PageCount * PageNum)
                SetVendorTypesVModel.IsCanView = true;
            else
                SetVendorTypesVModel.IsCanView = false;
            return SetVendorTypesVModel;
        }

        private SetupVendorTypesGridBData InitSearchVendorType(SetupVendorTypesGridViewModel Data)
        {
            SetupVendorTypesGridBData SrchData = new SetupVendorTypesGridBData
            {
                VendorTypeCode = Data.VendorTypeCodeText,
                VendorTypeDescription = Data.VendorTypeDescriptionText,
                PageNum = Data.PageNum,
                SelectedActive=Data.IsActiveSearch,
            };
            return SrchData;
        }
        private SetupVendorTypesGridBData InitSetupVendorTypesGridBData(SetupVendorTypesGridViewModel data)
        {
            SetupVendorTypesGridBData SetVendorTypesBData = new SetupVendorTypesGridBData();
            SetVendorTypesBData.Rows = new List<SetupVendorTypesGridRowBData>();
            foreach (var VendorType in data.Rows)
                SetVendorTypesBData.Rows.Add(new SetupVendorTypesGridRowBData
                {
                    VendorTypeID = VendorType.VendorTypeID,
                    VendorTypeCode = VendorType.VendorTypeCode != null ? VendorType.VendorTypeCode : "-",
                    VendorTypeDescription = VendorType.VendorTypeDescription,
                    Active=VendorType.IsActive,
                });
            return SetVendorTypesBData;
        }
        private SetupVendorTypesGridRowBData InitVendorTypeDetailsBData(SetupVendorTypesGridRowViewModel data)
        {
            SetupVendorTypesGridRowBData VendorTypeBData = new SetupVendorTypesGridRowBData
            {
                VendorTypeCode = data.VendorTypeCode != null ? data.VendorTypeCode : "-",
                VendorTypeDescription = data.VendorTypeDescription != null ? data.VendorTypeDescription : "-",
                Active=data.IsActive,
            };
            return VendorTypeBData;
        }
        #endregion
        #endregion
        #region DetailTypeController
        [Authorize]
        [HttpGet]
        public ActionResult SetupDetailType()
        {
            SetupManager = new SetupManager();
            var SetupDetailTypesData = SetupManager.LoadDetailTypes(1);
            SetupDetailTypesGridViewModel SetupDetailTypesView = new SetupDetailTypesGridViewModel();
            SetupDetailTypesView.VendorTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var vti in SetupDetailTypesData.VendorTypeID)
                SetupDetailTypesView.VendorTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = vti.Value, Value = vti.Key.ToString() });
            SetupDetailTypesView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupDetailTypesData.Active)
                SetupDetailTypesView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupDetailTypesView, AllPermVModel);
            return View("SetupDetailTypes", SetupDetailTypesView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadDetailTypesGrid(SetupDetailTypesGridViewModel model)
        {
            SetupManager = new SetupManager();

            SetupDetailTypesGridBData SetupDetailTypesData = new SetupDetailTypesGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteDetailTypeById(model.DetailTypeID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupDetailTypesGridBData SearchDetailTypesData = new SetupDetailTypesGridBData();
                SearchDetailTypesData = InitSearchDetailType(model);
                SetupDetailTypesData = SetupManager.LoadSearchedDetailTypes(SearchDetailTypesData);
            }
            else
                SetupDetailTypesData = SetupManager.LoadDetailTypes(model.PageNum);
            ModelState.Clear();
            var SetupDetailTypesView = InitSetupDetailTypesVModel(SetupDetailTypesData, model.PageNum);
            this.SetPermission(SetupDetailTypesView, AllPermVModel);
            return PartialView("LoadDetailTypesGrid", SetupDetailTypesView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewDetailType()
        {
            SetupManager = new SetupManager();
            SetupDetailTypesGridViewModel DetailTypeDetData = new SetupDetailTypesGridViewModel();
            DetailTypeDetData.Rows = new List<SetupDetailTypesGridRowViewModel>();
            DetailTypeDetData.Rows.Add(new SetupDetailTypesGridRowViewModel
            {
                DetailTypeDescription = "",
                DetailTypeID = 0,
                VendorTypeID = 0,
                IsActive = true,
            });
            var SetupVendorTypesData = SetupManager.LoadDetailTypes(-1);
            DetailTypeDetData.SelectedDetailVendorTypeIDText = "";
            DetailTypeDetData.VendorTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var vti in SetupVendorTypesData.VendorTypeID)
                DetailTypeDetData.VendorTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = vti.Value, Value = vti.Key.ToString() });
            ModelState.Clear();
            return PartialView("AddDetailTypeGrid", DetailTypeDetData);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveDetailTypeDetailData(SetupDetailTypesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var DetailTypeData = InitSetupDetailTypesGridBData(model);
            var EditError = SetupManager.SaveDetailTypeDetailsData(DetailTypeData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewDetailType(SetupDetailTypesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var DetailTypeData = InitDetailTypeDetailsBData(model);
            var CreateError = SetupManager.CreateDetailType(DetailTypeData);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupDetailTypesGridViewModel InitSetupDetailTypesVModel(SetupDetailTypesGridBData SetupDetailTypesData, int PageNum)
        {
            SetupDetailTypesGridViewModel SetDetailTypesVModel = new SetupDetailTypesGridViewModel();
            SetDetailTypesVModel.Rows = new List<SetupDetailTypesGridRowViewModel>();
            foreach (var DetailType in SetupDetailTypesData.Rows)
                SetDetailTypesVModel.Rows.Add(new SetupDetailTypesGridRowViewModel
                {
                    DetailTypeID = DetailType.DetailTypeID,
                    VendorTypeID = DetailType.DetailVendorTypeID,
                    DetailTypeDescription = DetailType.DetailTypeDescription,
                    IsActive=DetailType.Active !=null ? Convert.ToBoolean(DetailType.Active):false,
                });
            SetDetailTypesVModel.VendorTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var vti in SetupDetailTypesData.VendorTypeID)
                SetDetailTypesVModel.VendorTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = vti.Value, Value = vti.Key.ToString() });
            SetDetailTypesVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupDetailTypesData.Active)
                SetDetailTypesVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (SetDetailTypesVModel.Rows.Count == SetupDetailTypesGridBData.PageCount * PageNum)
                SetDetailTypesVModel.IsCanView = true;
            else
                SetDetailTypesVModel.IsCanView = false;
            return SetDetailTypesVModel;
        }

        private SetupDetailTypesGridBData InitSearchDetailType(SetupDetailTypesGridViewModel Data)
        {
            SetupDetailTypesGridBData SrchData = new SetupDetailTypesGridBData
            {
                DetailVendorTypeID = Convert.ToInt32(Data.SelectedDetailVendorTypeIDText),
                DetailTypeDescription = Data.DetailTypeDescriptionText,
                SelectedActive=Data.IsActiveSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private SetupDetailTypesGridBData InitSetupDetailTypesGridBData(SetupDetailTypesGridViewModel data)
        {
            SetupDetailTypesGridBData SetDetailTypesBData = new SetupDetailTypesGridBData();
            SetDetailTypesBData.Rows = new List<SetupDetailTypesGridRowBData>();
            foreach (var DetailType in data.Rows)
                SetDetailTypesBData.Rows.Add(new SetupDetailTypesGridRowBData
                {
                    DetailTypeID = DetailType.DetailTypeID,
                    DetailVendorTypeID = DetailType.VendorTypeID,
                    DetailTypeDescription = DetailType.DetailTypeDescription,
                    Active=DetailType.IsActive,
                });
            return SetDetailTypesBData;
        }
        private SetupDetailTypesGridBData InitDetailTypeDetailsBData(SetupDetailTypesGridViewModel data)
        {
            SetupDetailTypesGridBData DetailTypeBData = new SetupDetailTypesGridBData();
            DetailTypeBData.Rows = new List<SetupDetailTypesGridRowBData>();
            DetailTypeBData.Rows.Add(new SetupDetailTypesGridRowBData
            {
                DetailVendorTypeID = data.Rows[0].VendorTypeID,
                DetailTypeDescription = data.Rows[0].DetailTypeDescription,
                Active=data.Rows[0].IsActive,
            });
            return DetailTypeBData;
        }
        #endregion
        #endregion
        #region DetailController
        [Authorize]
        [HttpGet]
        public ActionResult SetupDetail()
        {
            SetupManager = new SetupManager();
            var SetupDetailsData = SetupManager.LoadDetails(1);
            SetupDetailsGridViewModel SetupDetailsView = new SetupDetailsGridViewModel();
            SetupDetailsView.SelectedDetailDetailTypeIDText = "";
            SetupDetailsView.DetailTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var DetailType in SetupDetailsData.DetailTypeID)
                SetupDetailsView.DetailTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = DetailType.Value, Value = DetailType.Key.ToString() });
            SetupDetailsView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupDetailsData.Active)
                SetupDetailsView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupDetailsView, AllPermVModel);
            return View("SetupDetails", SetupDetailsView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadDetailsGrid(SetupDetailsGridViewModel model)
        {
            SetupManager = new SetupManager();

            SetupDetailsGridBData Data = new SetupDetailsGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteDetailById(model.DetailID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupDetailsGridBData Search = new SetupDetailsGridBData();
                Search = InitSearchDetail(model);
                Data = SetupManager.LoadSearchedDetails(Search);
            }
            else
                Data = SetupManager.LoadDetails(model.PageNum);
            ModelState.Clear();
            var SetupDetailsView = InitSetupDetailsVModel(Data, model.PageNum);
            this.SetPermission(SetupDetailsView, AllPermVModel);
            return PartialView("LoadDetailsGrid", SetupDetailsView);
        }
        [Authorize]
        [HttpPost]
        public ActionResult GetServiceByDetailID(int DetailID)
        {
            SetupManager = new SetupManager();
            var data = SetupManager.GetServiceTypes(DetailID);
            List<System.Web.WebPages.Html.SelectListItem> ServiceTypesVModel = new List<System.Web.WebPages.Html.SelectListItem>();;
            foreach(var ServiceTypes in data)
            {
                ServiceTypesVModel.Add(new System.Web.WebPages.Html.SelectListItem() { Text = ServiceTypes.Value, Value = ServiceTypes.Key.ToString() });

            }
            return Json(ServiceTypesVModel);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewDetail()
        {
            SetupManager = new SetupManager();
            SetupDetailsGridViewModel Data = new SetupDetailsGridViewModel();
            Data.Rows = new List<SetupDetailsGridRowViewModel>();
            Data.Rows.Add(new SetupDetailsGridRowViewModel
            {
                DetailDescription = "",
                DetailCode = "",
                VendFlg = false,
                IsActive = true,
            });
            var SetupDetailsData = SetupManager.LoadDetails(-1);
            Data.Rows[0].ServiceTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var ServiceTypes in SetupDetailsData.Rows[0].ServiceTypeID)
                Data.Rows[0].ServiceTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = ServiceTypes.Value, Value = ServiceTypes.Key.ToString() });
            Data.SelectedDetailDetailTypeIDText = "";
            Data.DetailTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var DetailType in SetupDetailsData.DetailTypeID)
                Data.DetailTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = DetailType.Value, Value = DetailType.Key.ToString() });
            ModelState.Clear();
            return PartialView("AddDetailGrid", Data);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveDetailDetailData(SetupDetailsGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSetupDetailsGridBData(model);
            var EditError = SetupManager.SaveDetailDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewDetail(SetupDetailsGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitDetailDetailsBData(model);
            var CreateError = SetupManager.CreateDetail(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupDetailsGridViewModel InitSetupDetailsVModel(SetupDetailsGridBData Data, int PageNum)
        {
            SetupDetailsGridViewModel SetVModel = new SetupDetailsGridViewModel();
            SetVModel.Rows = new List<SetupDetailsGridRowViewModel>();
            foreach (var Detail in Data.Rows)
                SetVModel.Rows.Add(new SetupDetailsGridRowViewModel
                {
                    DetailID = Detail.DetailID,
                    DetailCode = Detail.DetailCode,
                    DetailDescription = Detail.DetailDescription,
                    SelectedDetailServiceTypeID = Detail.DetailServiceTypeID != null ? Detail.DetailServiceTypeID.ToString() : "",
                    DetailTypeID =  Detail.DetailTypeID.ToString(),
                    AddrFlg = Detail.AddrFlg != null ? Convert.ToBoolean(Detail.AddrFlg) : false,
                    GuidFlg = Detail.GuidFlg != null ? Convert.ToBoolean(Detail.GuidFlg) : false,
                    TimeFlg = Detail.TimeFlg != null ? Convert.ToBoolean(Detail.TimeFlg) : false,
                    VendFlg = Detail.VendFlg != null ? Convert.ToBoolean(Detail.VendFlg) : false,
                    IsActive = Detail.Active != null ? Convert.ToBoolean(Detail.Active) : false,

                });
            int ind = 0;
            foreach (var ServiceTypeBD in Data.Rows)
            {
                SetVModel.Rows[ind].ServiceTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
                foreach (var ServiceTypes in ServiceTypeBD.ServiceTypeID)
                    SetVModel.Rows[ind].ServiceTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = ServiceTypes.Value, Value = ServiceTypes.Key.ToString() });
                ++ind;
            }
            SetVModel.DetailTypeID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var DetailType in Data.DetailTypeID)
                SetVModel.DetailTypeID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = DetailType.Value, Value = DetailType.Key.ToString() });
            SetVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                SetVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (SetVModel.Rows.Count == SetupDetailsGridBData.PageCount * PageNum)
                SetVModel.IsCanView = true;
            else
                SetVModel.IsCanView = false;
            return SetVModel;
        }
        private SetupDetailsGridBData InitSearchDetail(SetupDetailsGridViewModel Data)
        {
            SetupDetailsGridBData SrchData = new SetupDetailsGridBData
            {
                DetailDetailTypeID = Data.SelectedDetailDetailTypeIDText != null ? Convert.ToInt32(Data.SelectedDetailDetailTypeIDText) : (int?)null,
                DetailCode = Data.DetailCodeText,
                DetailDescription = Data.DetailDescriptionText,
                PageNum = Data.PageNum,
                SelectedActive=Data.IsActiveSearch,
            };
            return SrchData;
        }
        private SetupDetailsGridBData InitSetupDetailsGridBData(SetupDetailsGridViewModel data)
        {
            SetupDetailsGridBData SetBData = new SetupDetailsGridBData();
            SetBData.Rows = new List<SetupDetailsGridRowBData>();
            foreach (var Detail in data.Rows)
                SetBData.Rows.Add(new SetupDetailsGridRowBData
                {
                    DetailID=Detail.DetailID,
                    DetailServiceTypeID = Detail.SelectedDetailServiceTypeID != null ? Convert.ToInt32(Detail.SelectedDetailServiceTypeID) : (int?)null,
                    DetailTypeID = Convert.ToInt32(Detail.DetailTypeID),
                    DetailCode = Detail.DetailCode != null ? Detail.DetailCode : "-",
                    DetailDescription = Detail.DetailDescription != null ? Detail.DetailDescription : "-",
                    TimeFlg = Detail.TimeFlg,
                    GuidFlg = Detail.GuidFlg,
                    VendFlg = Detail.VendFlg,
                    AddrFlg = Detail.AddrFlg,
                    Active=Detail.IsActive,
                });
            return SetBData;
        }
        private SetupDetailsGridBData InitDetailDetailsBData(SetupDetailsGridViewModel data)
        {
            SetupDetailsGridBData BData = new SetupDetailsGridBData();
            BData.Rows = new List<SetupDetailsGridRowBData>();
            BData.Rows.Add(new SetupDetailsGridRowBData
            {
                DetailServiceTypeID = data.Rows[0].SelectedDetailServiceTypeID != null ? Convert.ToInt32(data.Rows[0].SelectedDetailServiceTypeID) : (int?)null,
                DetailTypeID = Convert.ToInt32(data.Rows[0].DetailTypeID),
                DetailCode = data.Rows[0].DetailCode != null ? data.Rows[0].DetailCode : "-",
                DetailDescription = data.Rows[0].DetailDescription != null ? data.Rows[0].DetailDescription : "-",
                TimeFlg = data.Rows[0].TimeFlg,
                GuidFlg = data.Rows[0].GuidFlg,
                VendFlg = data.Rows[0].VendFlg,
                AddrFlg = data.Rows[0].AddrFlg,
                Active=data.Rows[0].IsActive,
            });
            return BData;
        }
        #endregion
        #endregion
        #region RegionController
        [Authorize]
        [HttpGet]
        public ActionResult SetupRegion()
        {
            SetupManager = new SetupManager();
            var SetupRegionsData = SetupManager.LoadRegions(1);
            SetupRegionsGridViewModel SetupRegionsView = new SetupRegionsGridViewModel();
            SetupRegionsView.Country = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cnt in SetupRegionsData.Country)
                SetupRegionsView.Country.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cnt.Value, Value = cnt.Key.ToString() });
            SetupRegionsView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupRegionsData.Active)
                SetupRegionsView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupRegionsView, AllPermVModel);
            return View("SetupRegions", SetupRegionsView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadRegionsGrid(SetupRegionsGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupRegionsGridBData SetupRegionsData = new SetupRegionsGridBData();
            if(model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteRegionById(model.RegionID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupRegionsGridBData SearchRegionsData = new SetupRegionsGridBData();
                SearchRegionsData = InitSearchRegion(model);
                SetupRegionsData = SetupManager.LoadSearchedRegions(SearchRegionsData);
            }
            else
                SetupRegionsData = SetupManager.LoadRegions(model.PageNum);
            ModelState.Clear();
            var SetupRegionsView = InitSetupRegionsVModel(SetupRegionsData, model.PageNum);
            this.SetPermission(SetupRegionsView, AllPermVModel);
            return PartialView("LoadRegionsGrid", SetupRegionsView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewRegion()
        {
            SetupManager = new SetupManager();
            SetupRegionsGridViewModel Data = new SetupRegionsGridViewModel();
            Data.Rows = new List<SetupRegionsGridRowViewModel>();
            Data.Rows.Add(new SetupRegionsGridRowViewModel
            {
                CountryID = null,
                IsActive = true,
            });
            var SetupRegionsData = SetupManager.LoadRegions(-1);
            Data.Country = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cnt in SetupRegionsData.Country)
                Data.Country.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cnt.Value, Value = cnt.Key.ToString() });
            ModelState.Clear();
            return PartialView("AddRegionGrid", Data);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveRegionDetailData(SetupRegionsGridViewModel model)
        {
            SetupManager = new SetupManager();
            var RegionData = InitSetupRegionsGridBData(model);
            var EditError = SetupManager.SaveRegionDetailsData(RegionData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewRegion(SetupRegionsGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitRegionDetailsBData(model);
            var CreateError = SetupManager.CreateRegion(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupRegionsGridViewModel InitSetupRegionsVModel(SetupRegionsGridBData SetupRegionsData, int PageNum)
        {
            SetupRegionsGridViewModel SetRegionsVModel = new SetupRegionsGridViewModel();
            SetRegionsVModel.Rows = new List<SetupRegionsGridRowViewModel>();
            foreach (var Region in SetupRegionsData.Rows)
                SetRegionsVModel.Rows.Add(new SetupRegionsGridRowViewModel
                {
                    RegionID = Region.RegionID,
                    CountryID = Region.RegionCountryID.ToString(),
                    RegionCode = Region.RegionCode,
                    RegionDescription = Region.RegionDescription,
                    IsActive=Region.Active !=null ? Convert.ToBoolean(Region.Active):false,
                });
            SetRegionsVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupRegionsData.Active)
                SetRegionsVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            SetRegionsVModel.Country = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cnt in SetupRegionsData.Country)
                SetRegionsVModel.Country.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cnt.Value, Value = cnt.Key.ToString() });
            if (SetRegionsVModel.Rows.Count == SetupRegionsGridBData.PageCount * PageNum)
                SetRegionsVModel.IsCanView = true;
            else
                SetRegionsVModel.IsCanView = false;
            return SetRegionsVModel;
        }

        private SetupRegionsGridBData InitSearchRegion(SetupRegionsGridViewModel Data)
        {
            SetupRegionsGridBData SrchData = new SetupRegionsGridBData
            {
                RegionCountryID = Convert.ToInt32(Data.SelectedRegionCountryIDText),
                RegionCode = Data.RegionCodeText,
                RegionDescription = Data.RegionDescriptionText,
                SelectedActive=Data.IsActiveSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private SetupRegionsGridBData InitSetupRegionsGridBData(SetupRegionsGridViewModel data)
        {
            SetupRegionsGridBData SetRegionsBData = new SetupRegionsGridBData();
            SetRegionsBData.Rows = new List<SetupRegionsGridRowBData>();
            foreach (var Region in data.Rows)
                SetRegionsBData.Rows.Add(new SetupRegionsGridRowBData
                {
                    RegionID = Region.RegionID,
                    RegionCode = Region.RegionCode != null ? Region.RegionCode : "-",
                    RegionCountryID = Region.CountryID != null ? Convert.ToInt32(Region.CountryID) : (int?)null,
                    RegionDescription = Region.RegionDescription,
                    Active=Region.IsActive,
                });
            return SetRegionsBData;
        }
        private SetupRegionsGridBData InitRegionDetailsBData(SetupRegionsGridViewModel data)
        {
            SetupRegionsGridBData RegionBData = new SetupRegionsGridBData();
            RegionBData.Rows = new List<SetupRegionsGridRowBData>();
            RegionBData.Rows.Add(new SetupRegionsGridRowBData
            {
                RegionCountryID = data.Rows[0].CountryID != null ? Convert.ToInt32(data.Rows[0].CountryID) : (int?)null,
                RegionCode = data.Rows[0].RegionCode,
                RegionDescription = data.Rows[0].RegionDescription,
                Active = data.Rows[0].IsActive,
            });
            return RegionBData;
        }
        #endregion
        #endregion
        #region AreaController
        [Authorize]
        [HttpGet]
        public ActionResult SetupArea()
        {
            SetupManager = new SetupManager();
            var SetupAreasData = SetupManager.LoadAreas(1);
            SetupAreasGridViewModel SetupAreasView = new SetupAreasGridViewModel();
            SetupAreasView.RegionID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var region in SetupAreasData.RegionID)
                SetupAreasView.RegionID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = region.Value, Value = region.Key.ToString() });
            SetupAreasView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupAreasData.Active)
                SetupAreasView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupAreasView, AllPermVModel);
            return View("SetupAreas", SetupAreasView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadAreasGrid(SetupAreasGridViewModel model)
        {
            SetupManager = new SetupManager();

            SetupAreasGridBData Data = new SetupAreasGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteAreaById(model.AreaID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupAreasGridBData Search = new SetupAreasGridBData();
                Search = InitSearchArea(model);
                Data = SetupManager.LoadSearchedAreas(Search);
            }
            else
                Data = SetupManager.LoadAreas(model.PageNum);
            ModelState.Clear();
            var SetupAreasView = InitSetupAreasVModel(Data, model.PageNum);
            this.SetPermission(SetupAreasView, AllPermVModel);
            return PartialView("LoadAreasGrid", SetupAreasView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewArea()
        {
            SetupManager = new SetupManager();
            SetupAreasGridViewModel Data = new SetupAreasGridViewModel();
            Data.Rows = new List<SetupAreasGridRowViewModel>();
            Data.Rows.Add(new SetupAreasGridRowViewModel
            {
                RegionID = null,
                IsActive = true,
            });
            var SetupRegionsData = SetupManager.LoadAreas(-1);
            Data.RegionID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Region in SetupRegionsData.RegionID)
                Data.RegionID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Region.Value, Value = Region.Key.ToString() });
            ModelState.Clear();
            return PartialView("AddAreaGrid", Data);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveAreaDetailData(SetupAreasGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSetupAreasGridBData(model);
            var EditError = SetupManager.SaveAreaDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewArea(SetupAreasGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitAreaDetailsBData(model);
            var CreateError = SetupManager.CreateArea(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupAreasGridViewModel InitSetupAreasVModel(SetupAreasGridBData Data, int PageNum)
        {
            SetupAreasGridViewModel SetVModel = new SetupAreasGridViewModel();
            SetVModel.Rows = new List<SetupAreasGridRowViewModel>();
            foreach (var Area in Data.Rows)
                SetVModel.Rows.Add(new SetupAreasGridRowViewModel
                {
                    AreaID = Area.AreaID,
                    RegionID = Area.AreaRegionID.ToString(),
                    AreaCode = Area.AreaCode,
                    AreaDescription = Area.AreaDescription,
                    IsActive=Area.Active!=null?Convert.ToBoolean(Area.Active):false,
                });
            SetVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                SetVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            SetVModel.RegionID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Region in Data.RegionID)
                SetVModel.RegionID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Region.Value, Value = Region.Key.ToString() });
            if (SetVModel.Rows.Count == SetupAreasGridBData.PageCount * PageNum)
                SetVModel.IsCanView = true;
            else
                SetVModel.IsCanView = false;
            return SetVModel;
        }
        private SetupAreasGridBData InitSearchArea(SetupAreasGridViewModel Data)
        {
            SetupAreasGridBData SrchData = new SetupAreasGridBData
            {
                AreaRegionID = Convert.ToInt32(Data.SelectedAreaRegionIDText),
                AreaCode = Data.AreaCodeText,
                AreaDescription = Data.AreaDescriptionText,
                PageNum = Data.PageNum,
                SelectedActive=Data.IsActiveSearch,
            };
            return SrchData;
        }
        private SetupAreasGridBData InitSetupAreasGridBData(SetupAreasGridViewModel data)
        {
            SetupAreasGridBData SetBData = new SetupAreasGridBData();
            SetBData.Rows = new List<SetupAreasGridRowBData>();
            foreach (var Area in data.Rows)
                SetBData.Rows.Add(new SetupAreasGridRowBData
                {
                    AreaID = Area.AreaID,
                    AreaRegionID = Area.RegionID != null ?Convert.ToInt32(Area.RegionID) : (int?)null,
                    AreaCode = Area.AreaCode,
                    AreaDescription = Area.AreaDescription,
                    Active=Area.IsActive,
                });
            return SetBData;
        }
        private SetupAreasGridBData InitAreaDetailsBData(SetupAreasGridViewModel data)
        {
            SetupAreasGridBData BData = new SetupAreasGridBData();
            BData.Rows = new List<SetupAreasGridRowBData>();
            BData.Rows.Add(new SetupAreasGridRowBData
            {
                AreaRegionID = data.Rows[0].RegionID != null ? Convert.ToInt32(data.Rows[0].RegionID) : (int?)null,
                AreaCode = data.Rows[0].AreaCode,
                AreaDescription = data.Rows[0].AreaDescription,
                Active=data.Rows[0].IsActive,
            });
            return BData;
        }
        #endregion
        #endregion
        #region StateController
        [Authorize]
        [HttpGet]
        public ActionResult SetupState()
        {
            SetupManager = new SetupManager();
            var SetupStatesData = SetupManager.LoadStates(1);
            SetupStatesGridViewModel SetupStatesView = new SetupStatesGridViewModel();
            SetupStatesView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupStatesData.Active)
                SetupStatesView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupStatesView, AllPermVModel);
            return View("SetupStates", SetupStatesView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadStatesGrid(SetupStatesGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupStatesGridBData SetupStatesData = new SetupStatesGridBData();
            if(model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteStateById(model.StID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupStatesGridBData SearchStatesData = new SetupStatesGridBData();
                SearchStatesData = InitSearchState(model);
                SetupStatesData = SetupManager.LoadSearchedStates(SearchStatesData);
            }
            else
                SetupStatesData = SetupManager.LoadStates(model.PageNum);
            ModelState.Clear();
            var SetupStatesView = InitSetupStatesVModel(SetupStatesData, model.PageNum);
            this.SetPermission(SetupStatesView, AllPermVModel);
            return PartialView("LoadStatesGrid", SetupStatesView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewState()
        {
            SetupStatesGridRowViewModel StateDetData = new SetupStatesGridRowViewModel();
            StateDetData.IsActive = true;
            ModelState.Clear();
            return PartialView("AddStateGrid", StateDetData);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveStateDetailData(SetupStatesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var StateData = InitSetupStatesGridBData(model);
            var EditError = SetupManager.SaveStateDetailsData(StateData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewState(SetupStatesGridRowViewModel model)
        {
            SetupManager = new SetupManager();
            var StateData = InitStateDetailsBData(model);
            var CreateError = SetupManager.CreateState(StateData);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupStatesGridViewModel InitSetupStatesVModel(SetupStatesGridBData SetupStatesData, int PageNum)
        {
            SetupStatesGridViewModel SetStatesVModel = new SetupStatesGridViewModel();
            SetStatesVModel.Rows = new List<SetupStatesGridRowViewModel>();
            foreach (var State in SetupStatesData.Rows)
                SetStatesVModel.Rows.Add(new SetupStatesGridRowViewModel
                {
                    StID=State.StID,
                    StateID = State.StateID,
                    StateName = State.StateName,
                    IsActive=State.Active!=null ? Convert.ToBoolean(State.Active):false,
                });
            if (SetStatesVModel.Rows.Count == SetupRegionsGridBData.PageCount * PageNum)
                SetStatesVModel.IsCanView = true;
            else
                SetStatesVModel.IsCanView = false;
            return SetStatesVModel;
        }

        private SetupStatesGridBData InitSearchState(SetupStatesGridViewModel Data)
        {
            SetupStatesGridBData SrchData = new SetupStatesGridBData
            {
                StateID = Data.StateIDText,
                StateName = Data.StateNameText,
                PageNum = Data.PageNum,
                SelectedActive=Data.IsActiveSearch,
            };
            return SrchData;
        }
        private SetupStatesGridBData InitSetupStatesGridBData(SetupStatesGridViewModel data)
        {
            SetupStatesGridBData SetStatesBData = new SetupStatesGridBData();
            SetStatesBData.Rows = new List<SetupStatesGridRowBData>();
            foreach (var State in data.Rows)
                SetStatesBData.Rows.Add(new SetupStatesGridRowBData
                {
                    StID=State.StID,
                    StateID = State.StateID != null ? State.StateID : "-",
                    StateName = State.StateName,
                    Active=State.IsActive,
                });
            return SetStatesBData;
        }
        private SetupStatesGridRowBData InitStateDetailsBData(SetupStatesGridRowViewModel data)
        {
            SetupStatesGridRowBData StateBData = new SetupStatesGridRowBData
            {
                StID=data.StID,
                StateID = data.StateID != null ? data.StateID : "-",
                StateName = data.StateName,
                Active=data.IsActive,
            };
            return StateBData;
        }
        #endregion
        #endregion
        #region CountryController
        [Authorize]
        [HttpGet]
        public ActionResult SetupCountry()
        {
            SetupManager = new SetupManager();
            var Data = SetupManager.LoadCountries(1);
            SetupCountriesGridViewModel CountriesView = new SetupCountriesGridViewModel();
            CountriesView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                CountriesView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(CountriesView, AllPermVModel);
            return View("SetupCountries", CountriesView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadCountriesGrid(SetupCountriesGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupCountriesGridBData Data = new SetupCountriesGridBData();
            if(model.IsDelete)
            {
               var DeleteError = SetupManager.DeleteCountryById(model.CountryID);
               if (DeleteError.errorCode != 0)
               {
                   MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                   {
                       Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                   };
                   return PartialView("MessageBox", MessBoxforDelete);
               }
            }
            if (model.IsSearch)
            {
                SetupCountriesGridBData Search = new SetupCountriesGridBData();
                Search = InitSearchCountry(model);
                Data = SetupManager.LoadSearchedCountries(Search);
            }
            else
                Data = SetupManager.LoadCountries(model.PageNum);
            ModelState.Clear();
            var CountriesView = InitSetupCountriesVModel(Data, model.PageNum);
            this.SetPermission(CountriesView, AllPermVModel);
            return PartialView("LoadCountriesGrid", CountriesView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewCountry()
        {
            SetupCountriesGridRowViewModel Data = new SetupCountriesGridRowViewModel();
            Data.IsActive = true;
            ModelState.Clear();
            return PartialView("AddCountryGrid", Data);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveCountryDetailData(SetupCountriesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSetupCountriesGridBData(model);
            var EditError = SetupManager.SaveCountryDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewCountry(SetupCountriesGridRowViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitCountryDetailsBData(model);
            var CreateError = SetupManager.CreateCountry(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupCountriesGridViewModel InitSetupCountriesVModel(SetupCountriesGridBData BData, int PageNum)
        {
            SetupCountriesGridViewModel VModelData = new SetupCountriesGridViewModel();
            VModelData.Rows = new List<SetupCountriesGridRowViewModel>();
            foreach (var Country in BData.Rows)
                VModelData.Rows.Add(new SetupCountriesGridRowViewModel
                {
                    CountryID = Country.CountryID,
                    CountryName = Country.CountryName,
                    CountryCode = Country.CountryCode,
                    Nationality = Country.Nationality,
                    IsActive=Country.Active !=null ? Convert.ToBoolean(Country.Active):false,
                });
            VModelData.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in BData.Active)
                VModelData.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (VModelData.Rows.Count == SetupCountriesGridBData.PageCount * PageNum)
                VModelData.IsCanView = true;
            else
                VModelData.IsCanView = false;
            return VModelData;
        }
        private SetupCountriesGridBData InitSearchCountry(SetupCountriesGridViewModel Data)
        {
            SetupCountriesGridBData SrchData = new SetupCountriesGridBData
            {
                CountryCode = Data.CountryCodeText,
                CountryName = Data.CountryNameText,
                Nationality = Data.NationalityText,
                SelectedActive=Data.IsActiveSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private SetupCountriesGridBData InitSetupCountriesGridBData(SetupCountriesGridViewModel data)
        {
            SetupCountriesGridBData SetBData = new SetupCountriesGridBData();
            SetBData.Rows = new List<SetupCountriesGridRowBData>();
            foreach (var Country in data.Rows)
                SetBData.Rows.Add(new SetupCountriesGridRowBData
                {
                    CountryID = Country.CountryID,
                    CountryCode = Country.CountryCode != null ? Country.CountryCode : "-",
                    CountryName = Country.CountryName != null ? Country.CountryName : "-",
                    Nationality = Country.Nationality != null ? Country.Nationality : "-",
                    Active=Country.IsActive,
                });
            return SetBData;
        }
        private SetupCountriesGridRowBData InitCountryDetailsBData(SetupCountriesGridRowViewModel data)
        {
            SetupCountriesGridRowBData BData = new SetupCountriesGridRowBData
            {
                CountryCode = data.CountryCode != null ? data.CountryCode : "-",
                CountryName = data.CountryName != null ? data.CountryName : "-",
                Nationality = data.Nationality != null ? data.Nationality : "-",
                Active=data.IsActive,
            };
            return BData;
        }
        #endregion
        #endregion
        #region CityController
        [Authorize]
        [HttpGet]
        public ActionResult SetupCity()
        {
            SetupManager = new SetupManager();
            var DataBD = SetupManager.LoadCities(1);
            SetupCitiesGridViewModel ModelView = new SetupCitiesGridViewModel();
            ModelView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in DataBD.Active)
                ModelView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelView.CountryID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cnt in DataBD.CountryID)
                ModelView.CountryID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cnt.Value, Value = cnt.Key.ToString() });

            ModelView.StateID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var state in DataBD.State)
                ModelView.StateID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = state.Value, Value = state.Key });
            ModelState.Clear();
            this.SetPermission(ModelView, AllPermVModel);
            return View("SetupCities", ModelView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadCitiesGrid(SetupCitiesGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupCitiesGridBData SetupCitiesData = new SetupCitiesGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteCityById(model.CityID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupCitiesGridBData SearchCitiesData = new SetupCitiesGridBData();
                SearchCitiesData = InitSearchCity(model);
                SetupCitiesData = SetupManager.LoadSearchedCities(SearchCitiesData);
            }
            else
                SetupCitiesData = SetupManager.LoadCities(model.PageNum);
            ModelState.Clear();
            var ModelView = InitSetupCitiesVModel(SetupCitiesData, model.PageNum);
            this.SetPermission(ModelView, AllPermVModel);
            return PartialView("LoadCitiesGrid", ModelView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewCity()
        {
            SetupManager = new SetupManager();
            SetupCitiesGridRowBData SetupAddCitiesData = new SetupCitiesGridRowBData();
            SetupAddCitiesData = SetupManager.LoadAddCityDetails();
            SetupCitiesGridRowViewModel Data = new SetupCitiesGridRowViewModel();
            ModelState.Clear();
            return PartialView("AddCityGrid", InitSetupAddCitiesVModel(SetupAddCitiesData));
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveCityDetailData(SetupCitiesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSetupCitiesGridBData(model);
            var EditError = SetupManager.SaveCityDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewCity(SetupCitiesGridRowViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitCityDetailsBData(model);
            var CreateError = SetupManager.CreateCity(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupCitiesGridRowViewModel InitSetupAddCitiesVModel(SetupCitiesGridRowBData Data)
        {
            SetupCitiesGridRowViewModel VModel = new SetupCitiesGridRowViewModel();
            VModel.CountryID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cnt in Data.CountryID)
                VModel.CountryID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cnt.Value, Value = cnt.Key.ToString() });

            VModel.StateID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var state in Data.State)
                VModel.StateID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = state.Value, Value = state.Key });
            VModel.IsActive = true;
            return VModel;
        }
        private SetupCitiesGridViewModel InitSetupCitiesVModel(SetupCitiesGridBData Data, int PageNum)
        {
            SetupCitiesGridViewModel VModel = new SetupCitiesGridViewModel();
            VModel.Rows = new List<SetupCitiesGridRowViewModel>();
            foreach (var City in Data.Rows)
                VModel.Rows.Add(new SetupCitiesGridRowViewModel
                {
                    CityID = City.CityID,
                    SelectedCountryID = City.SelectedCountry.ToString(),
                    SelectedStateID = City.SelectedState,
                    City = City.City,
                    IsActive = City.Active != null ? Convert.ToBoolean(City.Active) : false,
                });
            VModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                VModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });

            VModel.CountryID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cnt in Data.CountryID)
                VModel.CountryID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cnt.Value, Value = cnt.Key.ToString() });

            VModel.StateID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var state in Data.State)
                VModel.StateID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = state.Value, Value = state.Key});

            if (VModel.Rows.Count == SetupCitiesGridBData.PageCount * PageNum)
                VModel.IsCanView = true;
            else
                VModel.IsCanView = false;
            return VModel;
        }
        private SetupCitiesGridBData InitSearchCity(SetupCitiesGridViewModel Data)
        {
            SetupCitiesGridBData SrchData = new SetupCitiesGridBData
            {
                City = Data.CityText,
                SelectedCountry = (Data.SelectedCountryText != null || Data.SelectedCountryText != "") ? Convert.ToInt32(Data.SelectedCountryText) : 0,
                SelectedState = (Convert.ToInt32(Data.SelectedCountryText) == 1 && Data.SelectedStateText != "" && Data.SelectedStateText!=null) ? Data.SelectedStateText : "",
                SelectedActive = Data.IsActiveSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private SetupCitiesGridBData InitSetupCitiesGridBData(SetupCitiesGridViewModel data)
        {
            SetupCitiesGridBData BData = new SetupCitiesGridBData();
            BData.Rows = new List<SetupCitiesGridRowBData>();
            foreach (var City in data.Rows)
                BData.Rows.Add(new SetupCitiesGridRowBData
                {
                    CityID = City.CityID,
                    SelectedCountry = Convert.ToInt32(City.SelectedCountryID),
                    SelectedState = City.SelectedCountryID=="1" ? City.SelectedStateID : null,
                    City = City.City != null ? City.City : "-",
                    Active = City.IsActive,
                });
            return BData;
        }
        private SetupCitiesGridRowBData InitCityDetailsBData(SetupCitiesGridRowViewModel data)
        {
            SetupCitiesGridRowBData BData = new SetupCitiesGridRowBData
            {
                City = data.City != null ? data.City : "-",
                SelectedCountry = Convert.ToInt32(data.SelectedCountryID),
                SelectedState = data.SelectedCountryID == "1" ? data.SelectedStateID : null,
                Active = data.IsActive,
            };
            return BData;
        }
        #endregion
        #endregion
        #region QuotaRegionController
        [Authorize]
        [HttpGet]
        public ActionResult SetupQuotaRegion()
        {
            SetupManager = new SetupManager();
            var DataBD = SetupManager.LoadQuotaRegions(1);
            SetupQuotaRegionsGridViewModel ModelView = new SetupQuotaRegionsGridViewModel();
            ModelView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in DataBD.Active)
                ModelView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(ModelView, AllPermVModel);
            return View("SetupQuotaRegions", ModelView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadQuotaRegionsGrid(SetupQuotaRegionsGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupQuotaRegionsGridBData SetupQuotaRegionsData = new SetupQuotaRegionsGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteQuotaRegionById(model.RegionID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupQuotaRegionsGridBData SearchQuotaRegionsData = new SetupQuotaRegionsGridBData();
                SearchQuotaRegionsData = InitSearchQuotaRegion(model);
                SetupQuotaRegionsData = SetupManager.LoadSearchedQuotaRegions(SearchQuotaRegionsData);
            }
            else
                SetupQuotaRegionsData = SetupManager.LoadQuotaRegions(model.PageNum);
            ModelState.Clear();
            var ModelView = InitSetupQuotaRegionsVModel(SetupQuotaRegionsData, model.PageNum);
            this.SetPermission(ModelView, AllPermVModel);
            return PartialView("LoadQuotaRegionsGrid", ModelView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewQuotaRegion()
        {
            SetupQuotaRegionsGridRowViewModel Data = new SetupQuotaRegionsGridRowViewModel();
            Data.IsActive = true;
            ModelState.Clear();
            return PartialView("AddQuotaRegionGrid", Data);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveQuotaRegionDetailData(SetupQuotaRegionsGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSetupQuotaRegionsGridBData(model);
            var EditError = SetupManager.SaveQuotaRegionDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewQuotaRegion(SetupQuotaRegionsGridRowViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitQuotaRegionDetailsBData(model);
            var CreateError = SetupManager.CreateQuotaRegion(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupQuotaRegionsGridViewModel InitSetupQuotaRegionsVModel(SetupQuotaRegionsGridBData Data, int PageNum)
        {
            SetupQuotaRegionsGridViewModel VModel = new SetupQuotaRegionsGridViewModel();
            VModel.Rows = new List<SetupQuotaRegionsGridRowViewModel>();
            foreach (var QuotaRegion in Data.Rows)
                VModel.Rows.Add(new SetupQuotaRegionsGridRowViewModel
                {
                    RegionID = QuotaRegion.RegionID,
                    Region = QuotaRegion.Region,
                    IsActive = QuotaRegion.Active != null ? Convert.ToBoolean(QuotaRegion.Active) : false,
                });
            VModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                VModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (VModel.Rows.Count == SetupQuotaRegionsGridBData.PageCount * PageNum)
                VModel.IsCanView = true;
            else
                VModel.IsCanView = false;
            return VModel;
        }
        private SetupQuotaRegionsGridBData InitSearchQuotaRegion(SetupQuotaRegionsGridViewModel Data)
        {
            SetupQuotaRegionsGridBData SrchData = new SetupQuotaRegionsGridBData
            {
                Region = Data.RegionText,
                SelectedActive = Data.IsActiveSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private SetupQuotaRegionsGridBData InitSetupQuotaRegionsGridBData(SetupQuotaRegionsGridViewModel data)
        {
            SetupQuotaRegionsGridBData BData = new SetupQuotaRegionsGridBData();
            BData.Rows = new List<SetupQuotaRegionsGridRowBData>();
            foreach (var QuotaRegion in data.Rows)
                BData.Rows.Add(new SetupQuotaRegionsGridRowBData
                {
                    RegionID = QuotaRegion.RegionID,
                    Region = QuotaRegion.Region != null ? QuotaRegion.Region : "-",
                    Active = QuotaRegion.IsActive,
                });
            return BData;
        }
        private SetupQuotaRegionsGridRowBData InitQuotaRegionDetailsBData(SetupQuotaRegionsGridRowViewModel data)
        {
            SetupQuotaRegionsGridRowBData BData = new SetupQuotaRegionsGridRowBData
            {
                Region = data.Region != null ? data.Region : "-",
                Active = data.IsActive,
            };
            return BData;
        }
        #endregion
        #endregion
        #region CardController
        [Authorize]
        [HttpGet]
        public ActionResult SetupCard()
        {
            SetupManager = new SetupManager();
            var SetupCardsData = SetupManager.LoadCards(1);
            SetupCardsGridViewModel SetupCardsView = new SetupCardsGridViewModel();
            SetupCardsView.CardType = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cardtype in SetupCardsData.CardType)
                SetupCardsView.CardType.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cardtype.Value, Value = cardtype.Key.ToString() });
            SetupCardsView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupCardsData.Active)
                SetupCardsView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupCardsView, AllPermVModel);
            return View("SetupCards", SetupCardsView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadCardsGrid(SetupCardsGridViewModel model)
        {
            SetupManager = new SetupManager();

            SetupCardsGridBData Data = new SetupCardsGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteCardById(model.CardID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupCardsGridBData Search = new SetupCardsGridBData();
                Search = InitSearchCard(model);
                Data = SetupManager.LoadSearchedCards(Search);
            }
            else
                Data = SetupManager.LoadCards(model.PageNum);
            ModelState.Clear();
            var SetupCardsView = InitSetupCardsVModel(Data, model.PageNum);
            this.SetPermission(SetupCardsView, AllPermVModel);
            return PartialView("LoadCardsGrid", SetupCardsView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewCard()
        {
            SetupManager = new SetupManager();
            SetupCardsGridViewModel Data = new SetupCardsGridViewModel();
            Data.Rows = new List<SetupCardsGridRowViewModel>();
            Data.Rows.Add(new SetupCardsGridRowViewModel
            {
                CardTypeID = null,
                IsActive = true,
            });
            var SetupCardsData = SetupManager.LoadCards(-1);
            Data.CardType = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cardtype in SetupCardsData.CardType)
                Data.CardType.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cardtype.Value, Value = cardtype.Key.ToString() });
            ModelState.Clear();
            return PartialView("AddCardGrid", Data);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveCardDetailData(SetupCardsGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSetupCardsGridBData(model);
            var EditError = SetupManager.SaveCardDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewCard(SetupCardsGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitCardDetailsBData(model);
            var CreateError = SetupManager.CreateCard(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupCardsGridViewModel InitSetupCardsVModel(SetupCardsGridBData Data, int PageNum)
        {
            SetupCardsGridViewModel SetVModel = new SetupCardsGridViewModel();
            SetVModel.Rows = new List<SetupCardsGridRowViewModel>();
            foreach (var card in Data.Rows)
                SetVModel.Rows.Add(new SetupCardsGridRowViewModel
                {
                    CardID = card.CardID,
                    CardTypeID = card.CardTypeID.ToString(),
                    CardNumber = card.CardNumber,
                    PlaceHolderName = card.PlaceHolderName,
                    CardValidFromDate = card.CardValidFromDate != null ? Convert.ToDateTime(card.CardValidFromDate).ToString("dd/MMM/yy") : "",
                    CardValidToDate = card.CardValidToDate != null ? Convert.ToDateTime(card.CardValidToDate).ToString("dd/MMM/yy") : "",
                    IsActive = card.Active != null ? Convert.ToBoolean(card.Active) : false,
                });
            SetVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                SetVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            SetVModel.CardType = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var cardtype in Data.CardType)
                SetVModel.CardType.Add(new System.Web.WebPages.Html.SelectListItem() { Text = cardtype.Value, Value = cardtype.Key.ToString() });
            if (SetVModel.Rows.Count == SetupCardsGridBData.PageCount * PageNum)
                SetVModel.IsCanView = true;
            else
                SetVModel.IsCanView = false;
            return SetVModel;
        }
        private SetupCardsGridBData InitSearchCard(SetupCardsGridViewModel Data)
        {
            SetupCardsGridBData SrchData = new SetupCardsGridBData
            {
                CardTypeID = Convert.ToInt32(Data.SelectedCardTypeIDText),
                CardNumber = Data.CardNumberText,
                PlaceHolderName = Data.PlaceHolderNameText,
                PageNum = Data.PageNum,
                SelectedActive = Data.IsActiveSearch,
            };
            return SrchData;
        }
        private SetupCardsGridBData InitSetupCardsGridBData(SetupCardsGridViewModel data)
        {
            SetupCardsGridBData SetBData = new SetupCardsGridBData();
            SetBData.Rows = new List<SetupCardsGridRowBData>();
            foreach (var card in data.Rows)
                SetBData.Rows.Add(new SetupCardsGridRowBData
                {
                    CardID = card.CardID,
                    CardTypeID = card.CardTypeID != null ? Convert.ToInt32(card.CardTypeID) : 1,
                    CardNumber = card.CardNumber !=null ? card.CardNumber : "-",
                    PlaceHolderName = card.PlaceHolderName != null ? card.PlaceHolderName : "-",
                    CardValidFromDate = card.CardValidFromDate != null ? Convert.ToDateTime(card.CardValidFromDate) : DateTime.Now,
                    CardValidToDate = card.CardValidToDate != null ? Convert.ToDateTime(card.CardValidToDate) : DateTime.Now,
                    Active = card.IsActive,
                });
            return SetBData;
        }
        private SetupCardsGridBData InitCardDetailsBData(SetupCardsGridViewModel data)
        {
            SetupCardsGridBData BData = new SetupCardsGridBData();
            BData.Rows = new List<SetupCardsGridRowBData>();
            BData.Rows.Add(new SetupCardsGridRowBData
            {
                CardTypeID = data.Rows[0].CardTypeID != null ? Convert.ToInt32(data.Rows[0].CardTypeID) : 1,
                CardNumber = data.Rows[0].CardNumber != null ? data.Rows[0].CardNumber : "-",
                PlaceHolderName = data.Rows[0].PlaceHolderName != null ? data.Rows[0].PlaceHolderName : "-",
                CardValidFromDate = data.Rows[0].CardValidFromDate != null ? Convert.ToDateTime(data.Rows[0].CardValidFromDate) : DateTime.Now,
                CardValidToDate = data.Rows[0].CardValidToDate != null ? Convert.ToDateTime(data.Rows[0].CardValidToDate) : DateTime.Now,
                Active = data.Rows[0].IsActive,
            });
            return BData;
        }
        #endregion
        #endregion
        #region TechTypeController
        [Authorize]
        [HttpGet]
        public ActionResult SetupTechType()
        {
            SetupManager = new SetupManager();
            var SetupTechTypesData = SetupManager.LoadTechTypes(1);
            SetupTechTypesGridViewModel SetupTechTypesView = new SetupTechTypesGridViewModel();
            SetupTechTypesView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupTechTypesData.Active)
                SetupTechTypesView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupTechTypesView, AllPermVModel);
            return View("SetupTechTypes", SetupTechTypesView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadTechTypesGrid(SetupTechTypesGridViewModel model)
        {
            SetupManager = new SetupManager();
            SetupTechTypesGridBData SetupTechTypesData = new SetupTechTypesGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteTechTypeById(model.TechTypeID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupTechTypesGridBData SearchTechTypesData = new SetupTechTypesGridBData();
                SearchTechTypesData = InitSearchTechType(model);
                SetupTechTypesData = SetupManager.LoadSearchedTechTypes(SearchTechTypesData);
            }
            else
                SetupTechTypesData = SetupManager.LoadTechTypes(model.PageNum);
            ModelState.Clear();
            var SetupTechTypesView = InitSetupTechTypesVModel(SetupTechTypesData, model.PageNum);
            this.SetPermission(SetupTechTypesView, AllPermVModel);
            return PartialView("LoadTechTypesGrid", SetupTechTypesView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewTechType()
        {
            SetupTechTypesGridRowViewModel TechTypeDetData = new SetupTechTypesGridRowViewModel();
            TechTypeDetData.IsActive = true;
            ModelState.Clear();
            return PartialView("AddTechTypeGrid", TechTypeDetData);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveTechTypeDetailData(SetupTechTypesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var TechTypeData = InitSetupTechTypesGridBData(model);
            var EditError = SetupManager.SaveTechTypeDetailsData(TechTypeData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewTechType(SetupTechTypesGridRowViewModel model)
        {
            SetupManager = new SetupManager();
            var TechTypeData = InitTechTypeDetailsBData(model);
            var CreateError = SetupManager.CreateTechType(TechTypeData);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupTechTypesGridViewModel InitSetupTechTypesVModel(SetupTechTypesGridBData SetupTechTypesData, int PageNum)
        {
            SetupTechTypesGridViewModel SetTechTypesVModel = new SetupTechTypesGridViewModel();
            SetTechTypesVModel.Rows = new List<SetupTechTypesGridRowViewModel>();
            foreach (var TechType in SetupTechTypesData.Rows)
                SetTechTypesVModel.Rows.Add(new SetupTechTypesGridRowViewModel
                {
                    TechTypeID = TechType.TechTypeID,
                    TechTypeCode = TechType.TechTypeCode,
                    TechTypeDescription = TechType.TechTypeDescription,
                    IsActive = TechType.Active != null ? Convert.ToBoolean(TechType.Active) : false,
                });
            SetTechTypesVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupTechTypesData.Active)
                SetTechTypesVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (SetTechTypesVModel.Rows.Count == SetupRegionsGridBData.PageCount * PageNum)
                SetTechTypesVModel.IsCanView = true;
            else
                SetTechTypesVModel.IsCanView = false;
            return SetTechTypesVModel;
        }

        private SetupTechTypesGridBData InitSearchTechType(SetupTechTypesGridViewModel Data)
        {
            SetupTechTypesGridBData SrchData = new SetupTechTypesGridBData
            {
                TechTypeCode = Data.TechTypeCodeText,
                TechTypeDescription = Data.TechTypeDescriptionText,
                SelectedActive = Data.IsActiveSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private SetupTechTypesGridBData InitSetupTechTypesGridBData(SetupTechTypesGridViewModel data)
        {
            SetupTechTypesGridBData SetTechTypesBData = new SetupTechTypesGridBData();
            SetTechTypesBData.Rows = new List<SetupTechTypesGridRowBData>();
            foreach (var TechType in data.Rows)
                SetTechTypesBData.Rows.Add(new SetupTechTypesGridRowBData
                {
                    TechTypeID = TechType.TechTypeID,
                    TechTypeCode = TechType.TechTypeCode != null ? TechType.TechTypeCode : "-",
                    TechTypeDescription = TechType.TechTypeDescription,
                    Active = TechType.IsActive,
                });
            return SetTechTypesBData;
        }
        private SetupTechTypesGridRowBData InitTechTypeDetailsBData(SetupTechTypesGridRowViewModel data)
        {
            SetupTechTypesGridRowBData TechTypeBData = new SetupTechTypesGridRowBData
            {
                TechTypeCode = data.TechTypeCode != null ? data.TechTypeCode : "-",
                TechTypeDescription = data.TechTypeDescription,
                Active = data.IsActive,
            };
            return TechTypeBData;
        }
        #endregion
        #endregion
        #region VConfStatusController
        [Authorize]
        [HttpGet]
        public ActionResult SetupVConfStatus()
        {
            SetupManager = new SetupManager();
            var SetupVConfStatusData = SetupManager.LoadVConfStatus(1);
            SetupVConfStatusGridViewModel SetupVConfStatusView = new SetupVConfStatusGridViewModel();
            SetupVConfStatusView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupVConfStatusData.Active)
                SetupVConfStatusView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupVConfStatusView, AllPermVModel);
            return View("SetupVConfStatus", SetupVConfStatusView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadVConfStatusGrid(SetupVConfStatusGridViewModel model)
        {
          SetupManager = new SetupManager();
            SetupVConfStatusGridBData SetupVConfStatusData = new SetupVConfStatusGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteVConfStatusById(model.StatusID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupVConfStatusGridBData SearchVConfStatusData = new SetupVConfStatusGridBData();
                SearchVConfStatusData = InitSearchVConfStatus(model);
                SetupVConfStatusData = SetupManager.LoadSearchedVConfStatus(SearchVConfStatusData);
            }
            else
                SetupVConfStatusData = SetupManager.LoadVConfStatus(model.PageNum);
            ModelState.Clear();
            var SetupVConfStatusView = InitSetupVConfStatusVModel(SetupVConfStatusData, model.PageNum);
            this.SetPermission(SetupVConfStatusView, AllPermVModel);
            return PartialView("LoadVConfStatusGrid", SetupVConfStatusView);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewVConfStatus()
        {
            SetupVConfStatusGridRowViewModel VConfStatusDetData = new SetupVConfStatusGridRowViewModel();
            VConfStatusDetData.IsActive = true;
            ModelState.Clear();
            return PartialView("AddVConfStatusGrid", VConfStatusDetData);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveVConfStatusDetailData(SetupVConfStatusGridViewModel model)
        {
            SetupManager = new SetupManager();
            var VConfStatusData = InitSetupVConfStatusGridBData(model);
            var EditError = SetupManager.SaveVConfStatusDetailsData(VConfStatusData);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewVConfStatus(SetupVConfStatusGridRowViewModel model)
        {
            SetupManager = new SetupManager();
            var VConfStatusData = InitVConfStatusDetailsBData(model);
            var CreateError = SetupManager.CreateVConfStatus(VConfStatusData);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupVConfStatusGridViewModel InitSetupVConfStatusVModel(SetupVConfStatusGridBData SetupVConfStatusData, int PageNum)
        {
            SetupVConfStatusGridViewModel SetVConfStatusVModel = new SetupVConfStatusGridViewModel();
            SetVConfStatusVModel.Rows = new List<SetupVConfStatusGridRowViewModel>();
            foreach (var VCStatus in SetupVConfStatusData.Rows)
                SetVConfStatusVModel.Rows.Add(new SetupVConfStatusGridRowViewModel
                {
                    StatusID = VCStatus.StatusID,
                    StatusOrder = VCStatus.StatusOrder.ToString(),
                    StatusDesc = VCStatus.StatusDesc,
                    IsActive = VCStatus.Active != null ? Convert.ToBoolean(VCStatus.Active) : false,
                });
            SetVConfStatusVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupVConfStatusData.Active)
                SetVConfStatusVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (SetVConfStatusVModel.Rows.Count == SetupVConfStatusGridBData.PageCount * PageNum)
                SetVConfStatusVModel.IsCanView = true;
            else
                SetVConfStatusVModel.IsCanView = false;
            return SetVConfStatusVModel;
        }

        private SetupVConfStatusGridBData InitSearchVConfStatus(SetupVConfStatusGridViewModel Data)
        {
            SetupVConfStatusGridBData SrchData = new SetupVConfStatusGridBData
            {
                StatusDesc = Data.StatusDescText,
                SelectedActive = Data.IsActiveSearch,
                PageNum = Data.PageNum,
            };
            return SrchData;
        }
        private SetupVConfStatusGridBData InitSetupVConfStatusGridBData(SetupVConfStatusGridViewModel data)
        {
            SetupVConfStatusGridBData SetVConfStatusBData = new SetupVConfStatusGridBData();
            SetVConfStatusBData.Rows = new List<SetupVConfStatusGridRowBData>();
            foreach (var VCStatus in data.Rows)
                SetVConfStatusBData.Rows.Add(new SetupVConfStatusGridRowBData
                {
                    StatusID = VCStatus.StatusID,
                    StatusOrder = VCStatus.StatusOrder != null ? Convert.ToInt16(VCStatus.StatusOrder) : (short)0,
                    StatusDesc = VCStatus.StatusDesc,
                    Active = VCStatus.IsActive,
                });
            return SetVConfStatusBData;
        }
        private SetupVConfStatusGridRowBData InitVConfStatusDetailsBData(SetupVConfStatusGridRowViewModel data)
        {
            SetupVConfStatusGridRowBData VConfStatusBData = new SetupVConfStatusGridRowBData
            {
                StatusOrder = data.StatusOrder != null ? Convert.ToInt16(data.StatusOrder) : (short)0,
                StatusDesc = data.StatusDesc,
                Active = data.IsActive,
            };
            return VConfStatusBData;
        }
        #endregion
        #endregion
        #region SpecialDatesController
        [Authorize]
        [HttpGet]
        public ActionResult SetupSpecialDates()
        {
            SetupManager = new SetupManager();
            var SetupSpecialDatesData = SetupManager.LoadSpecialDates(1);
            SetupSpecialDatesGridViewModel SetupSpecialDatesView = new SetupSpecialDatesGridViewModel();
            SetupSpecialDatesView.SelectedSpecialDatesCityIDText = "";
            SetupSpecialDatesView.CityID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var City in SetupSpecialDatesData.CityID)
                SetupSpecialDatesView.CityID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = City.Value, Value = City.Key.ToString() });
            SetupSpecialDatesView.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in SetupSpecialDatesData.Active)
                SetupSpecialDatesView.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            ModelState.Clear();
            this.SetPermission(SetupSpecialDatesView, AllPermVModel);
            return View("SetupSpecialDates", SetupSpecialDatesView);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult LoadSpecialDatesGrid(SetupSpecialDatesGridViewModel model)
        {
            SetupManager = new SetupManager();

            SetupSpecialDatesGridBData Data = new SetupSpecialDatesGridBData();
            if (model.IsDelete)
            {
                var DeleteError = SetupManager.DeleteSpecialDateById(model.SpecialDatesID);
                if (DeleteError.errorCode != 0)
                {
                    MessageBoxViewModel MessBoxforDelete = new MessageBoxViewModel()
                    {
                        Message = DeleteError.Message != null ? "Error - " + DeleteError.Message : "",
                    };
                    return PartialView("MessageBox", MessBoxforDelete);
                }
            }
            if (model.IsSearch)
            {
                SetupSpecialDatesGridBData Search = new SetupSpecialDatesGridBData();
                Search = InitSearchSpecialDates(model);
                Data = SetupManager.LoadSearchedSpecialDates(Search);
            }
            else
                Data = SetupManager.LoadSpecialDates(model.PageNum);
            ModelState.Clear();
            var SetupSpecialDatesView = InitSetupSpecialDatesVModel(Data, model.PageNum);
            this.SetPermission(SetupSpecialDatesView, AllPermVModel);
            return PartialView("LoadSpecialDatesGrid", SetupSpecialDatesView);
        }
        [Authorize]
        [HttpPost]
        public ActionResult GetCity(int? CountryID)
        {
            SetupManager = new SetupManager();
            var data = SetupManager.GetCity(CountryID);
            List<System.Web.WebPages.Html.SelectListItem> ServiceTypesVModel = new List<System.Web.WebPages.Html.SelectListItem>(); ;
            foreach (var ServiceTypes in data)
            {
                ServiceTypesVModel.Add(new System.Web.WebPages.Html.SelectListItem() { Text = ServiceTypes.Value, Value = ServiceTypes.Key.ToString() });

            }
            return Json(ServiceTypesVModel);
        }
        [Authorize]
        [HttpGet]
        public PartialViewResult AddNewSpecialDate()
        {
            SetupManager = new SetupManager();
            SetupSpecialDatesGridViewModel Data = new SetupSpecialDatesGridViewModel();
            Data.Rows = new List<SetupSpecialDatesGridRowViewModel>();
            Data.Rows.Add(new SetupSpecialDatesGridRowViewModel
            {
                IsHotel = false,
                IsAttraction = false,
                IsRestaurant = false,
                IsMisc = false,
                IsBus = false,
                IsActive = true,
            });
            var SetupSpecialDatesData = SetupManager.LoadSpecialDates(-1);
            Data.Rows[0].CityID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var City in SetupSpecialDatesData.Rows[0].CityID)
                Data.Rows[0].CityID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = City.Value, Value = City.Key.ToString() });
            Data.CountryID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Country in SetupSpecialDatesData.CountryID)
                Data.CountryID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Country.Value, Value = Country.Key.ToString() });
            ModelState.Clear();
            return PartialView("AddSpecialDateGrid", Data);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult SaveSpecialDatesDetailData(SetupSpecialDatesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSetupSpecialDatesGridBData(model);
            var EditError = SetupManager.SaveSpecialDatesDetailsData(Data);
            MessageBoxViewModel MessBoxforEdit = new MessageBoxViewModel()
            {
                Message = EditError.Message != null ? "Error - " + EditError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforEdit);
        }
        [Authorize]
        [HttpPost]
        public PartialViewResult AddNewSpecialDate(SetupSpecialDatesGridViewModel model)
        {
            SetupManager = new SetupManager();
            var Data = InitSpecialDateDetailsBData(model);
            var CreateError = SetupManager.CreateSpecialDate(Data);
            MessageBoxViewModel MessBoxforCreate = new MessageBoxViewModel()
            {
                Message = CreateError.Message != null ? "Error - " + CreateError.Message : "",
            };
            return PartialView("MessageBox", MessBoxforCreate);
        }
        #region private methods
        private SetupSpecialDatesGridViewModel InitSetupSpecialDatesVModel(SetupSpecialDatesGridBData Data, int PageNum)
        {
            SetupSpecialDatesGridViewModel SetVModel = new SetupSpecialDatesGridViewModel();
            SetVModel.Rows = new List<SetupSpecialDatesGridRowViewModel>();
            foreach (var SpecialDate in Data.Rows)
                SetVModel.Rows.Add(new SetupSpecialDatesGridRowViewModel
                {
                    SpecialDatesID = SpecialDate.SpecialDatesID,
                    VendorID = SpecialDate.VendorID.ToString(),
                    VendorName = SpecialDate.VendorName,
                    CountryID = SpecialDate.CountryID.ToString(),
                    SelectedSpecialDatesCityID = SpecialDate.SpecialDatesCityID != null ? SpecialDate.SpecialDatesCityID.ToString() : "",
                    SpecialDatesStartDate = SpecialDate.StartDate != null ? Convert.ToDateTime(SpecialDate.StartDate).ToString("dd/MMM/yy") : "",
                    SpecialDatesEndDate = SpecialDate.EndDate != null ? Convert.ToDateTime(SpecialDate.EndDate).ToString("dd/MMM/yy") : "",
                    IsHotel = SpecialDate.IsHotel != null ? Convert.ToBoolean(SpecialDate.IsHotel) : false,
                    IsAttraction = SpecialDate.IsAttraction != null ? Convert.ToBoolean(SpecialDate.IsAttraction) : false,
                    IsRestaurant = SpecialDate.IsRestaurant != null ? Convert.ToBoolean(SpecialDate.IsRestaurant) : false,
                    IsMisc = SpecialDate.IsMisc != null ? Convert.ToBoolean(SpecialDate.IsMisc) : false,
                    IsBus = SpecialDate.IsBus != null ? Convert.ToBoolean(SpecialDate.IsBus) : false,
                    IsActive = SpecialDate.Active != null ? Convert.ToBoolean(SpecialDate.Active) : false,
                    SpecialDatesNotes = SpecialDate.Notes,
                });
            int ind = 0;
            foreach (var CityBD in Data.Rows)
            {
                SetVModel.Rows[ind].CityID = new List<System.Web.WebPages.Html.SelectListItem>();
                foreach (var City in CityBD.CityID)
                    SetVModel.Rows[ind].CityID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = City.Value, Value = City.Key.ToString() });
                ++ind;
            }
            SetVModel.CountryID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var Country in Data.CountryID)
                SetVModel.CountryID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = Country.Value, Value = Country.Key.ToString() });
            SetVModel.CityID = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var City in Data.CityID)
                SetVModel.CityID.Add(new System.Web.WebPages.Html.SelectListItem() { Text = City.Value, Value = City.Key.ToString() });
            SetVModel.ActiveSearch = new List<System.Web.WebPages.Html.SelectListItem>();
            foreach (var active in Data.Active)
                SetVModel.ActiveSearch.Add(new System.Web.WebPages.Html.SelectListItem() { Text = active.Value, Value = active.Key.ToString() });
            if (SetVModel.Rows.Count == SetupDetailsGridBData.PageCount * PageNum)
                SetVModel.IsCanView = true;
            else
                SetVModel.IsCanView = false;
            return SetVModel;
        }
        private SetupSpecialDatesGridBData InitSearchSpecialDates(SetupSpecialDatesGridViewModel Data)
        {
            SetupSpecialDatesGridBData SrchData = new SetupSpecialDatesGridBData
            {
                SpecialDatesCityID = Data.SelectedSpecialDatesCityIDText != null ? Convert.ToInt32(Data.SelectedSpecialDatesCityIDText) : (int?)null,
                StartDate = Data.SpecialDatesStartDateText != null ? Convert.ToDateTime(Data.SpecialDatesStartDateText) : (DateTime?)null,
                EndDate = Data.SpecialDatesEndDateText != null ? Convert.ToDateTime(Data.SpecialDatesEndDateText) : (DateTime?)null,
                PageNum = Data.PageNum,
                SelectedActive = Data.IsActiveSearch,
            };
            return SrchData;
        }
        private SetupSpecialDatesGridBData InitSetupSpecialDatesGridBData(SetupSpecialDatesGridViewModel data)
        {
            SetupSpecialDatesGridBData SetBData = new SetupSpecialDatesGridBData();
            SetBData.Rows = new List<SetupSpecialDatesGridRowBData>();
            foreach (var SpecialDate in data.Rows)
                SetBData.Rows.Add(new SetupSpecialDatesGridRowBData
                {
                    SpecialDatesID = SpecialDate.SpecialDatesID,
                    VendorID = SpecialDate.VendorID != null ? Convert.ToInt32(SpecialDate.VendorID) : (int?)null,
                    CountryID = SpecialDate.CountryID != null ? Convert.ToInt32(SpecialDate.CountryID) : (int?)null,
                    SpecialDatesCityID = SpecialDate.SelectedSpecialDatesCityID != null ? Convert.ToInt32(SpecialDate.SelectedSpecialDatesCityID) : (int?)null,
                    StartDate = SpecialDate.SpecialDatesStartDate != null ? Convert.ToDateTime(SpecialDate.SpecialDatesStartDate) : (DateTime?)null,
                    EndDate = SpecialDate.SpecialDatesEndDate != null ? Convert.ToDateTime(SpecialDate.SpecialDatesEndDate) : (DateTime?)null,
                    Notes = SpecialDate.SpecialDatesNotes,
                    IsHotel = SpecialDate.IsHotel,
                    IsAttraction = SpecialDate.IsAttraction,
                    IsRestaurant = SpecialDate.IsRestaurant,
                    IsMisc = SpecialDate.IsMisc,
                    IsBus = SpecialDate.IsBus,
                    Active = SpecialDate.IsActive,
                });
            return SetBData;
        }
        private SetupSpecialDatesGridBData InitSpecialDateDetailsBData(SetupSpecialDatesGridViewModel data)
        {
            SetupSpecialDatesGridBData BData = new SetupSpecialDatesGridBData();
            BData.Rows = new List<SetupSpecialDatesGridRowBData>();
            BData.Rows.Add(new SetupSpecialDatesGridRowBData
            {
                SpecialDatesCityID = data.Rows[0].SelectedSpecialDatesCityID != null ? Convert.ToInt32(data.Rows[0].SelectedSpecialDatesCityID) : (int?)null,
                StartDate = data.Rows[0].SpecialDatesStartDate != null ? Convert.ToDateTime(data.Rows[0].SpecialDatesStartDate) : (DateTime?)null,
                EndDate = data.Rows[0].SpecialDatesEndDate != null ? Convert.ToDateTime(data.Rows[0].SpecialDatesEndDate) : (DateTime?)null,
                CountryID = Convert.ToInt32(data.Rows[0].CountryID),
                VendorID = Convert.ToInt32(data.Rows[0].VendorID),
                Notes = data.Rows[0].SpecialDatesNotes,
                IsHotel = data.Rows[0].IsHotel,
                IsAttraction = data.Rows[0].IsAttraction,
                IsRestaurant = data.Rows[0].IsRestaurant,
                IsMisc = data.Rows[0].IsMisc,
                IsBus = data.Rows[0].IsBus,
                Active = data.Rows[0].IsActive,
            });
            return BData;
        }
        #endregion
        #endregion

        [Authorize]
        [HttpPost]
        public ActionResult GetStateByCountryID(int CountryID)
        {
            SetupManager = new SetupManager();
            var data = SetupManager.LoadState();
            List<System.Web.WebPages.Html.SelectListItem> StateViewModel = new List<System.Web.WebPages.Html.SelectListItem>(); ;
            foreach (var state in data)
            {
                StateViewModel.Add(new System.Web.WebPages.Html.SelectListItem() { Text = state.Value, Value = state.Key });

            }
            return Json(StateViewModel);
        }
        [Authorize]
        [HttpPost]
        public ActionResult GetCityByCountryID(int CountryID, string StateID)
        {
            SetupManager = new SetupManager();
            var data = SetupManager.LoadCity(CountryID, StateID);
            List<System.Web.WebPages.Html.SelectListItem> CityViewModel = new List<System.Web.WebPages.Html.SelectListItem>(); ;
            foreach (var city in data)
            {
                CityViewModel.Add(new System.Web.WebPages.Html.SelectListItem() { Text = city.Value, Value = city.Key.ToString() });

            }
            return Json(CityViewModel);
        }
        [Authorize]
        public PartialViewResult AutoComplete(string PropName, string keyword)
        {
            QuotationManager QuotaManager = new QuotationManager();
            var data = QuotaManager.FindKeywordInEntity(PropName, keyword);
            AutoCompleteViewModel AutoCompVMode = new AutoCompleteViewModel() { Items = data };
            return PartialView("QuotaAutoComplete", AutoCompVMode);
        }
    }
}